First look at the effect:
After the Repeater control is placed on the OnClick button of the ItemTemplate, the Item, ItemIndex, CommandArgument, CommandName, and bound field values of the Repeater are obtained.
Prepare data:
Copy codeThe Code is as follows:
View Code
Imports Microsoft. VisualBasic
Namespace Insus. NET
Public Class Catalog
Private _ ID As Integer
Private _ Name As String
Public Property ID As Integer
Get
Return _ ID
End Get
Set (value As Integer)
_ ID = value
End Set
End Property
Public Property Name As String
Get
Return _ Name
End Get
Set (value As String)
_ Name = value
End Set
End Property
End Class
End Namespace
Copy codeThe Code is as follows:
View Code
Private Function GetData () As List (Of Catalog)
Dim cls As New List (Of Catalog)
Dim cl As Catalog = New Catalog ()
Cl. ID = 1
Cl. Name = "Auto"
Cls. Add (cl)
Cl = New Catalog ()
Cl. ID = 2
Cl. Name = "fashion"
Cls. Add (cl)
Cl = New Catalog ()
Cl. ID = 3
Cl. Name = "tech"
Cls. Add (cl)
Cl = New Catalog ()
Cl. ID = 5
Cl. Name = "culture"
Cls. Add (cl)
Cl = New Catalog ()
Cl. ID = 6
Cl. Name = "public welfare"
Cls. Add (cl)
Return cls
End Function
Place the Repeater control in. aspx:
Copy codeThe Code is as follows:
View Code
<Asp: Repeater ID = "RepeaterCatalog" runat = "server">
<HeaderTemplate>
<Table border = "1" cellpadding = "3" cellspacing = "0">
<Tr>
<Td> ID
</Td>
<Td> Name
</Td>
<Td> Choose </td>
</Tr>
</HeaderTemplate>
<ItemTemplate>
<Tr>
<Td>
<Asp: Label ID = "LabelID" runat = "server" Text = '<% # Eval ("ID") %>'> </asp: Label>
</Td>
<Td>
<Asp: Label ID = "LabelName" runat = "server" Text = '<% # Eval ("Name") %>'> </asp: Label>
</Td>
<Td>
<Asp: Button ID = "Button1" runat = "server" Text = "Select" OnClick = "button#click" CommandArgument = '<% # Eval ("ID ") %> 'commandname = "Choose"/>
</Td>
</Tr>
</ItemTemplate>
<FooterTemplate>
</Table>
</FooterTemplate>
</Asp: Repeater>
Bind data to the Repeater control in. aspx. vb:
Copy codeThe Code is as follows:
View Code
Imports Insus. NET
Partial Class Default2
Inherits System. Web. UI. Page
Protected Sub Page_Load (sender As Object, e As EventArgs) Handles Me. Load
If Not IsPostBack Then
Data_Binding ()
End If
End Sub
Private Sub Data_Binding ()
Me. RepeaterCatalog. DataSource = GetData ()
Me. RepeaterCatalog. DataBind ()
End Sub
End Class
Next, we will write the onclick event. before writing the event, put a Label in. aspx to display the event result:
Copy codeThe Code is as follows:
Process infor:
<Asp: Label ID = "LabelInfo" runat = "server" Text = ""> </asp: Label>
Copy codeThe Code is as follows:
View Code
Protected Sub button#click (sender As Object, e As EventArgs)
Dim btn As Button = DirectCast (sender, Button)
Dim commandArgument As String = btn. CommandArgument
Dim commandName As String = btn. CommandName
Dim item As RepeaterItem = DirectCast (btn. NamingContainer, RepeaterItem)
Dim index As Integer = item. ItemIndex
Dim id As String = DirectCast (item. FindControl ("LabelID"), Label). Text
Dim name As String = DirectCast (item. FindControl ("LabelName"), Label). Text
Me. labelInfo. text = String. format ("Item index: {0}; CommandArgument: {1}; CommandName: {2}; ID: {3}; Name: {4};", index, commandArgument, commandName, id, name)
End Sub