Get Repeter's item and itemindex/commandargument implementation ideas and code _ practical Tips

Source: Internet
Author: User
first look at the effect

The Repeater control, placed in the ItemTemplate of the Ammonium button onclick, gets the item,itemindex,commandargument,commandname of the repeater and the value of the bound field.
Preparing Data
Copy Code code 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 Code code 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 = "Car"
Cls. ADD (CL)
CL = New Catalog ()
Cl.id = 2
Cl. Name = "Fashion"
Cls. ADD (CL)
CL = New Catalog ()
Cl.id = 3
Cl. Name = "Technology"
Cls. ADD (CL)
CL = New Catalog ()
Cl.id = 5
Cl. Name = "Culture"
Cls. ADD (CL)
CL = New Catalog ()
Cl.id = 6
Cl. Name = "Commonweal"
Cls. ADD (CL)
Return CLS
End Function

Place the Repeater control in. aspx:
Copy Code code 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= "Button1_Click" commandargument= "<%# Eval (" ID ")% > ' commandname= "Choose"/>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>

bind data to the Repeater control in. Aspx.vb
Copy Code code 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 write the onclick event and put a label in. aspx to display the event results before writing the event:
Copy Code code as follows:

Process infor:
<asp:label id= "Labelinfo" runat= "Server" text= "" ></asp:Label>

Copy Code code as follows:

View Code
Protected Sub button1_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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.