Control.NamingContainer 属性获取对服务器控件的命名容器的引用,此引用创建唯一的命名空间,以区分具有相同 Control.ID 属性值的服务器控件。
ASP.NET Web 应用程序的每一页均包含控件的层次结构。此层次结构与控件是否生成用户可见的 UI 无关。给定控件的命名容器是层次结构中该控件之上的父控件,此父控件实现 INamingContainer 接口。实现此接口的服务器控件为其子服务器控件的 ID 属性值创建唯一的命名空间。当针对列表 Web 服务器控件(如 Repeater 和 DataList 服务器控件)进行数据绑定时,为服务器控件创建唯一的命名空间尤其重要。当数据源中的多个项创建服务器控件的多个实例,且该服务器控件是重复控件的子级时,命名容器确保这些子控件的每个实例具有不冲突的 UniqueID 属性值。页的默认命名容器是请求该页时生成的 Page 类的实例。可以使用此属性确定特定服务器控件所在的命名容器。下面的示例说明如何使用 NamingContainer 属性。[Visual Basic]
<%@ Import Namespace = "System.Data" %>
<html>
<head>
<script language="VB" runat="server">
' Build the DataSource.
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
' Bind 'DataView' to the DataSource.
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">
<h3>
Control NamingContainer Example
</h3>
<h4>
Click an item to view it's Naming Container and UniqueID
</h4>
<asp:Label ID="myLabel1" Runat="server"></asp:Label>
<br>
<asp:Label ID="myLabel2" Runat="server"></asp:Label>
<br>
<asp:DataList 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>
</asp:DataList>
</form>
</body>
</html>
[C#]
<% @ Import Namespace = "System.Data" %>
<html>
<head>
<script language="C#" runat="server">
// Build the DataSource.
ICollection CreateDataSource()
{
DataTable myDataTable = new DataTable();
DataRow myDataRow;
myDataTable.Columns.Add(new DataColumn("EmployeeName", typeof(string)));
for (int i = 0; i < 10; i++)
{
myDataRow = myDataTable.NewRow();
myDataRow[0] = "somename" + i.ToString();
myDataTable.Rows.Add(myDataRow);
}
DataView myDataView = new DataView(myDataTable);
return myDataView;
} void Page_Load(Object sender, EventArgs e)
{
if (!IsPostBack)
{
// Bind 'DataView' to the DataSource.
myDataList.DataSource = CreateDataSource();
myDataList.DataBind();
}
// Attach EventHandler for SelectedIndexChanged event.
myDataList.SelectedIndexChanged += new EventHandler(selectedItemChanged);
} // Handler function for 'SelectedIndexChanged' event.
void selectedItemChanged(Object sender,EventArgs e)
{
DataListItem myCurrentItem = myDataList.SelectedItem;
Control myNamingContainer = myCurrentItem.Controls[0].NamingContainer;
// Display the NamingContainer.
myLabel1.Text = "The NamingContainer is : " + myNamingContainer.UniqueID;
// Display the UniqueID.
myLabel2.Text = "The UniqueID is : " + ((Control)(myCurrentItem.Controls[0])).UniqueID;
} </script>
</head>
<body>
<form runat="server" ID="Form1">
<h3>
Control NamingContainer Example
</h3>
<h4>
Click an item to view it's Naming Container and UniqueID
</h4>
<asp:Label ID="myLabel1" Runat="server"></asp:Label>
<br>
<asp:Label ID="myLabel2" Runat="server"></asp:Label>
<br>
<asp:DataList 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>
</asp:DataList>
</form>
</body>
</html>
ASP.NET Web 应用程序的每一页均包含控件的层次结构。此层次结构与控件是否生成用户可见的 UI 无关。给定控件的命名容器是层次结构中该控件之上的父控件,此父控件实现 INamingContainer 接口。实现此接口的服务器控件为其子服务器控件的 ID 属性值创建唯一的命名空间。当针对列表 Web 服务器控件(如 Repeater 和 DataList 服务器控件)进行数据绑定时,为服务器控件创建唯一的命名空间尤其重要。当数据源中的多个项创建服务器控件的多个实例,且该服务器控件是重复控件的子级时,命名容器确保这些子控件的每个实例具有不冲突的 UniqueID 属性值。页的默认命名容器是请求该页时生成的 Page 类的实例。可以使用此属性确定特定服务器控件所在的命名容器。下面的示例说明如何使用 NamingContainer 属性。[Visual Basic]
<%@ Import Namespace = "System.Data" %>
<html>
<head>
<script language="VB" runat="server">
' Build the DataSource.
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
' Bind 'DataView' to the DataSource.
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">
<h3>
Control NamingContainer Example
</h3>
<h4>
Click an item to view it's Naming Container and UniqueID
</h4>
<asp:Label ID="myLabel1" Runat="server"></asp:Label>
<br>
<asp:Label ID="myLabel2" Runat="server"></asp:Label>
<br>
<asp:DataList 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>
</asp:DataList>
</form>
</body>
</html>
[C#]
<% @ Import Namespace = "System.Data" %>
<html>
<head>
<script language="C#" runat="server">
// Build the DataSource.
ICollection CreateDataSource()
{
DataTable myDataTable = new DataTable();
DataRow myDataRow;
myDataTable.Columns.Add(new DataColumn("EmployeeName", typeof(string)));
for (int i = 0; i < 10; i++)
{
myDataRow = myDataTable.NewRow();
myDataRow[0] = "somename" + i.ToString();
myDataTable.Rows.Add(myDataRow);
}
DataView myDataView = new DataView(myDataTable);
return myDataView;
} void Page_Load(Object sender, EventArgs e)
{
if (!IsPostBack)
{
// Bind 'DataView' to the DataSource.
myDataList.DataSource = CreateDataSource();
myDataList.DataBind();
}
// Attach EventHandler for SelectedIndexChanged event.
myDataList.SelectedIndexChanged += new EventHandler(selectedItemChanged);
} // Handler function for 'SelectedIndexChanged' event.
void selectedItemChanged(Object sender,EventArgs e)
{
DataListItem myCurrentItem = myDataList.SelectedItem;
Control myNamingContainer = myCurrentItem.Controls[0].NamingContainer;
// Display the NamingContainer.
myLabel1.Text = "The NamingContainer is : " + myNamingContainer.UniqueID;
// Display the UniqueID.
myLabel2.Text = "The UniqueID is : " + ((Control)(myCurrentItem.Controls[0])).UniqueID;
} </script>
</head>
<body>
<form runat="server" ID="Form1">
<h3>
Control NamingContainer Example
</h3>
<h4>
Click an item to view it's Naming Container and UniqueID
</h4>
<asp:Label ID="myLabel1" Runat="server"></asp:Label>
<br>
<asp:Label ID="myLabel2" Runat="server"></asp:Label>
<br>
<asp:DataList 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>
</asp:DataList>
</form>
</body>
</html>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货