标题: 请问一个有关DataList的问题 [打印本页] 作者: netdragon 时间: 2004-2-27 03:51 标题: 请问一个有关DataList的问题 <%@ Page Language="VB" ContentType="text/html" ResponseEncoding="gb2312" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ Import Namespace = "System.Data" %>
<html>
<head>
<script language="VB" runat="server">
' 创建数据源
Function CreateDataSource() As ICollection
Dim myDataTable As New DataTable()
Dim myDataRow As DataRow
myDataTable.Columns.Add(New DataColumn("EmployeeName", GetType(String)))
Dim i As Integer
For i = 0 To 9
myDataRow = myDataTable.NewRow()
myDataRow(0) = "somename" + i.ToString()
myDataTable.Rows.Add(myDataRow)
Next i
Dim myDataView As New DataView(myDataTable)
Return myDataView
End Function
Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
'绑定数据到数据源
myDataList.DataSource = CreateDataSource()
myDataList.DataBind()
End If
' Attach EventHandler for SelectedIndexChanged event.
AddHandler myDataList.SelectedIndexChanged, AddressOf selectedItemChanged
End Sub
' Handler function for 'SelectedIndexChanged' event.
Sub selectedItemChanged(sender As Object, e As EventArgs)
Dim myCurrentItem As DataListItem = myDataList.SelectedItem
Dim myNamingContainer As Control = myCurrentItem.Controls(0).NamingContainer
' Display the NamingContainer.
myLabel1.Text = "The NamingContainer is : " + myNamingContainer.UniqueID
' Display the UniqueID.
myLabel2.Text = "The UniqueID is : " + CType(myCurrentItem.Controls(0), Control).UniqueID
End Sub
</script>
</head>
<body>
<form runat="server" ID="Form1">
<aspabel ID="myLabel1" Runat="server"></aspabel><br>
<aspabel ID="myLabel2" Runat="server"></asp:Label><br>
<aspataList id="myDataList" runat="server" BorderColor="black">
<HeaderStyle BackColor="#aaaadd"></HeaderStyle>
<SelectedItemStyle BackColor="lightgreen"></SelectedItemStyle>
<HeaderTemplate>
EmployeeName
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton id="button1" Text='<%# DataBinder.Eval(Container.DataItem, "EmployeeName") %>' CommandName="select" runat="server" />
</ItemTemplate>
</aspataList>
</form>
</body>
</html>
以上是源代码:请问代码Dim myNamingContainer As Control = myCurrentItem.Controls(0).NamingContainer中的0表示什么?还有代码CType(myCurrentItem.Controls(0), Control).UniqueID中的0表示什么?我改成1时myLabel2后面会显示LinkButton的id。请告诉我这两个数字的意思。跟DataList中的哪个子控件件有关?