Draft for Information Only
Content
How to: Access Members of Naming Container Access Members of a Web Server Control's Naming Container To access the naming container from a data-binding expression Reference Container keyword to the container Note Simple Repeater Web Server Control Examples Examples of Visual C# ASP.NET Repeater Web Server Control Examples of Visual Basic ASP.NET Repeater Web Server Control To access the naming container from code Reference the control's NamingContainer property See Also Concepts Reference Other Resources Source and Reference
How to: Access Members of Naming Container
Access Members of a Web Server Control's Naming Container
At times, you need to access properties or methods of a control's naming container. For example, during data binding, the naming container makes available a DataItem property containing the data to which controls are bound. You can access the containing control in different ways, depending on context.
To access the naming container from a data-binding expression
Reference Container keyword to the container
In the data-binding expression, use the Container keyword, which returns a reference to the container. You can then access the container's properties or methods.
This keyword is used most commonly in the Eval method to get values from the naming container's
DataItem object, but can be used outside of that method as well. The following example shows a
Label control that might be in a template for a
DataList ,
Repeater , or
GridView control. It displays the current item number followed by the current Title data item of the naming container.
<asp:Label ID="Label1" runat="server">
<%# Container.DataItemIndex + 1 %>. <%# Eval("Title") %>
</asp:Label>
The following example is similar, but gets a value (the Author item) from the naming container's DataItem object:
<asp:Label ID="Label2" runat="server" >
<%# DataBinder.Eval(Container.DataItem, "Author") %>
</asp:Label>
Note
The NamingContainer property does not necessarily reference the same control as the
Parent property. For example, in a
Repeater control, you might have an item template containing a table that in turn contains a
Label control. The parent control of the label is a table cell (for example, a
HtmlTableCell object), but its naming container is the
DataListItem object, because it is the
DataListItem that defines the namespace for the
Label control, not the table.
A complete example using the syntax shown above to get property values is shown below.
Simple Repeater Web Server Control Examples
Examples of Visual C# ASP.NET Repeater Web Server Control
ASP.NET Code Input: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head id="Head1" runat="server">
<title>Sample Page</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<%@ Page Language="C#" %>
</head>
<body>
<form id="form1" runat="server">
<h3>Naming Container Example</h3>
<div>
<asp:XmlDataSource ID="XmlDataSource1"
runat="server"
XPath="Books/LanguageBooks/Book">
<Data>
<Books>
<LanguageBooks>
<Book Title="Pure JavaScript"
Author="Wyke, Gilliam, and Ting"/>
<Book Title="Effective C++ Second Edition"
Author="Scott Meyers"/>
<Book Title="Assembly Language Step-By-Step"
Author="Jeff Duntemann"/>
<Book Title="Oracle PL/SQL"
Author="Steven Feuerstein"/>
</LanguageBooks>
<SecurityBooks>
<Book Title="Counter Hack"
Author="Ed Skoudis"/>
</SecurityBooks>
</Books>
</Data>
</asp:XmlDataSource>
<asp:GridView ID="GridView1"
runat="server"
DataSourceID="XmlDataSource1"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Title" >
<ItemTemplate>
<asp:Label ID="Label1" runat="server">
<%# Container.DataItemIndex + 1 %>. <%# Eval("Title") %>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Author">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" >
<%# DataBinder.Eval(Container.DataItem, "Author") %>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
HTTP Response Output: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head id="Head1"><title>
Sample Page
</title><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /></head>
<body>
<form method="post" action="./aspnetht_accessbynamingcontainer_001a_01.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTUzMDI5OTI5Mg9kFgICAw9kFgICAw88KwARAwAPFgQeC18hRGF0YUJvdW5kZx4LXyFJdGVtQ291bnQCBGQBEBYAFgAWAAwUKwAAFgJmD2QWCgIBD2QWBGYPZBYCAgEPZBYCZg8VAgExD1B1cmUgSmF2YVNjcmlwdGQCAQ9kFgICAQ9kFgJmDxUBF1d5a2UsIEdpbGxpYW0sIGFuZCBUaW5nZAICD2QWBGYPZBYCAgEPZBYCZg8VAgEyHEVmZmVjdGl2ZSBDKysgU2Vjb25kIEVkaXRpb25kAgEPZBYCAgEPZBYCZg8VAQxTY290dCBNZXllcnNkAgMPZBYEZg9kFgICAQ9kFgJmDxUCATMeQXNzZW1ibHkgTGFuZ3VhZ2UgU3RlcC1CeS1TdGVwZAIBD2QWAgIBD2QWAmYPFQEOSmVmZiBEdW50ZW1hbm5kAgQPZBYEZg9kFgICAQ9kFgJmDxUCATQNT3JhY2xlIFBML1NRTGQCAQ9kFgICAQ9kFgJmDxUBEVN0ZXZlbiBGZXVlcnN0ZWluZAIFDw8WAh4HVmlzaWJsZWhkZBgBBQlHcmlkVmlldzEPPCsADAEIAgFkzws5lZ/9P+YAPTfquPwjbARUHt+QTSeAAtv6eoqDufg=" />
</div>
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="83BE5E47" />
</div>
<h3>Naming Container Example</h3>
<div>
<div>
<table cellspacing="0" rules="all" border="1" id="GridView1" style="border-collapse:collapse;">
<tr>
<th scope="col">Title</th><th scope="col">Author</th>
</tr><tr>
<td>
<span id="GridView1_Label1_0">1. Pure JavaScript</span>
</td><td>
<span id="GridView1_Label2_0">Wyke, Gilliam, and Ting</span>
</td>
</tr><tr>
<td>
<span id="GridView1_Label1_1">2. Effective C++ Second Edition</span>
</td><td>
<span id="GridView1_Label2_1">Scott Meyers</span>
</td>
</tr><tr>
<td>
<span id="GridView1_Label1_2">3. Assembly Language Step-By-Step</span>
</td><td>
<span id="GridView1_Label2_2">Jeff Duntemann</span>
</td>
</tr><tr>
<td>
<span id="GridView1_Label1_3">4. Oracle PL/SQL</span>
</td><td>
<span id="GridView1_Label2_3">Steven Feuerstein</span>
</td>
</tr>
</table>
</div>
</div>
</form>
</body>
</html>
HTML Web Page Embedded Output:
Examples of Visual Basic ASP.NET Repeater Web Server Control
ASP.NET Code Input: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head id="Head1" runat="server">
<title>Sample Page</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<%@ Page Language="vb" %>
</head>
<body>
<form id="form1" runat="server">
<h3>Naming Container Example</h3>
<div>
<asp:XmlDataSource ID="XmlDataSource1"
runat="server"
XPath="Books/LanguageBooks/Book">
<Data>
<Books>
<LanguageBooks>
<Book Title="Pure JavaScript"
Author="Wyke, Gilliam, and Ting"/>
<Book Title="Effective C++ Second Edition"
Author="Scott Meyers"/>
<Book Title="Assembly Language Step-By-Step"
Author="Jeff Duntemann"/>
<Book Title="Oracle PL/SQL"
Author="Steven Feuerstein"/>
</LanguageBooks>
<SecurityBooks>
<Book Title="Counter Hack"
Author="Ed Skoudis"/>
</SecurityBooks>
</Books>
</Data>
</asp:XmlDataSource>
<asp:GridView ID="GridView1"
runat="server"
DataSourceID="XmlDataSource1"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Title" >
<ItemTemplate>
<asp:Label ID="Label1" runat="server">
<%# Container.DataItemIndex + 1 %>. <%# Eval("Title") %>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Author">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" >
<%# DataBinder.Eval(Container.DataItem, "Author") %>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
HTTP Response Output: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head id="Head1"><title>
Sample Page
</title><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /></head>
<body>
<form method="post" action="./aspnetht_accessbynamingcontainer_001a_02.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTUzMDI5OTI5Mg9kFgICAw9kFgICAw88KwARAwAPFgQeC18hRGF0YUJvdW5kZx4LXyFJdGVtQ291bnQCBGQBEBYAFgAWAAwUKwAAFgJmD2QWCgIBD2QWBGYPZBYCAgEPZBYCZg8VAgExD1B1cmUgSmF2YVNjcmlwdGQCAQ9kFgICAQ9kFgJmDxUBF1d5a2UsIEdpbGxpYW0sIGFuZCBUaW5nZAICD2QWBGYPZBYCAgEPZBYCZg8VAgEyHEVmZmVjdGl2ZSBDKysgU2Vjb25kIEVkaXRpb25kAgEPZBYCAgEPZBYCZg8VAQxTY290dCBNZXllcnNkAgMPZBYEZg9kFgICAQ9kFgJmDxUCATMeQXNzZW1ibHkgTGFuZ3VhZ2UgU3RlcC1CeS1TdGVwZAIBD2QWAgIBD2QWAmYPFQEOSmVmZiBEdW50ZW1hbm5kAgQPZBYEZg9kFgICAQ9kFgJmDxUCATQNT3JhY2xlIFBML1NRTGQCAQ9kFgICAQ9kFgJmDxUBEVN0ZXZlbiBGZXVlcnN0ZWluZAIFDw8WAh4HVmlzaWJsZWhkZBgBBQlHcmlkVmlldzEPPCsADAEIAgFkexX4ROdMFtsCwnhSBgRRELUqPBWB6lidCiuzPw9+Bis=" />
</div>
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="28F643BD" />
</div>
<h3>Naming Container Example</h3>
<div>
<div>
<table cellspacing="0" rules="all" border="1" id="GridView1" style="border-collapse:collapse;">
<tr>
<th scope="col">Title</th><th scope="col">Author</th>
</tr><tr>
<td>
<span id="GridView1_Label1_0">1. Pure JavaScript</span>
</td><td>
<span id="GridView1_Label2_0">Wyke, Gilliam, and Ting</span>
</td>
</tr><tr>
<td>
<span id="GridView1_Label1_1">2. Effective C++ Second Edition</span>
</td><td>
<span id="GridView1_Label2_1">Scott Meyers</span>
</td>
</tr><tr>
<td>
<span id="GridView1_Label1_2">3. Assembly Language Step-By-Step</span>
</td><td>
<span id="GridView1_Label2_2">Jeff Duntemann</span>
</td>
</tr><tr>
<td>
<span id="GridView1_Label1_3">4. Oracle PL/SQL</span>
</td><td>
<span id="GridView1_Label2_3">Steven Feuerstein</span>
</td>
</tr>
</table>
</div>
</div>
</form>
</body>
</html>
HTML Web Page Embedded Output:
To access the naming container from code
Reference the control's NamingContainer property
Get the control's
NamingContainer property and cast it to the container's class type, such as
GridViewRow .
For an example, see
Using the NamingContainer Property to Determine a Control's Naming Container .
See Also
Concepts
Web Forms Control Identification
Reference
Data-Binding Expression Syntax
Other Resources
Accessing ASP.NET Controls Programmatically
Source and Reference
https://docs.microsoft.com/en-us/previous-versions/aspnet/858twd77(v=vs.100)
©sideway
ID: 211000019 Last Updated: 10/19/2021 Revision: 0 Ref:
References
Active Server Pages, , http://msdn.microsoft.com/en-us/library/aa286483.aspx
ASP Overview, , http://msdn.microsoft.com/en-us/library/ms524929%28v=vs.90%29.aspx
ASP Best Practices, , http://technet.microsoft.com/en-us/library/cc939157.aspx
ASP Built-in Objects, , http://msdn.microsoft.com/en-us/library/ie/ms524716(v=vs.90).aspx
Response Object, , http://msdn.microsoft.com/en-us/library/ms525405(v=vs.90).aspx
Request Object, , http://msdn.microsoft.com/en-us/library/ms524948(v=vs.90).aspx
Server Object (IIS), , http://msdn.microsoft.com/en-us/library/ms525541(v=vs.90).aspx
Application Object (IIS), , http://msdn.microsoft.com/en-us/library/ms525360(v=vs.90).aspx
Session Object (IIS), , http://msdn.microsoft.com/en-us/library/ms524319(8v=vs.90).aspx
ASPError Object, , http://msdn.microsoft.com/en-us/library/ms524942(v=vs.90).aspx
ObjectContext Object (IIS), , http://msdn.microsoft.com/en-us/library/ms525667(v=vs.90).aspx
Debugging Global.asa Files, , http://msdn.microsoft.com/en-us/library/aa291249(v=vs.71).aspx
How to: Debug Global.asa files, , http://msdn.microsoft.com/en-us/library/ms241868(v=vs.80).aspx
Calling COM Components from ASP Pages, , http://msdn.microsoft.com/en-us/library/ms524620(v=VS.90).aspx
IIS ASP Scripting Reference, , http://msdn.microsoft.com/en-us/library/ms524664(v=vs.90).aspx
ASP Keywords, , http://msdn.microsoft.com/en-us/library/ms524672(v=vs.90).aspx
Creating Simple ASP Pages, , http://msdn.microsoft.com/en-us/library/ms524741(v=vs.90).aspx
Including Files in ASP Applications, , http://msdn.microsoft.com/en-us/library/ms524876(v=vs.90).aspx
ASP Overview, , http://msdn.microsoft.com/en-us/library/ms524929(v=vs.90).aspx
FileSystemObject Object, , http://msdn.microsoft.com/en-us/library/z9ty6h50(v=vs.84).aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/ms675944(v=vs.85).aspx, , ADO Object Model
ADO Fundamentals, , http://msdn.microsoft.com/en-us/library/windows/desktop/ms680928(v=vs.85).aspx