关于RepeaterCommandEventargs的问题.
nnh 2006-03-18 10:57:34 在学asp.net技术内幕这本书,有个例子中的一条语句不是很明白,请大家帮忙看看.
代码:
<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<Script Runat="Server">
Sub Page_Load
If Not IsPostBack Then
Dim conNorthwind As SqlConnection
Dim cmdSelect As SqlCommand
Dim dtrCategories As SqlDataReader
conNorthwind = New SqlConnection( "Server=localhost;UID=sa;PWD=;Database=Northwind" )
cmdSelect = New SqlCommand( "Select CategoryName From Categories", conNorthwind )
conNorthwind.Open()
dtrCategories = cmdSelect.ExecuteReader()
rptCategories.DataSource = dtrCategories
rptCategories.DataBind()
dtrCategories.Close()
conNorthwind.Close()
End If
End Sub
Sub Button_Click( s As Object, e As RepeaterCommandEventArgs )
Dim lbtnSelected As LinkButton
lbtnSelected = e.Item.Controls( 1 )
lblCategory.Text = "You selected " & lbtnSelected.Text
End Sub
</Script>
<html>
<head><title>LinkButtonList.aspx</title></head>
<body>
<form Runat="Server">
<asp:Repeater
ID="rptCategories"
OnItemCommand="Button_Click"
Runat="Server">
<ItemTemplate>
<li>
<asp:LinkButton
Text='<%# Container.DataItem( "CategoryName" ) %>'
Runat="Server" />
</ItemTemplate>
<ItemTemplate>
<li>
<asp:LinkButton
Text='<%# Container.DataItem( "CategoryName" ) %>'
Runat="Server" />
</ItemTemplate>
</asp:Repeater>
<p>
<asp:Label
ID="lblCategory"
Runat="Server" />
</form>
</body>
</html>
========================
其中的 lbtnSelected = e.Item.Controls( 1 ) ,为什么要用 1 呢?请大家帮忙解释一下,谢谢.