Compartilhar via


How to: 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

  • 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>
    
    <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>
    
    <asp:Label ID="Label2" runat="server" >
     <%# DataBinder.Eval(Container.DataItem, "Author") %>
    </asp:Label>
    
    NoteNote

    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.

    <%@ Page Language="VB" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Naming Container Example</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <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>
    
    <%@ Page Language="C#" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Naming Container Example</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <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>
    

To access the naming container from code

See Also

Reference

Data Binding Expression Syntax

Concepts

Web Forms Control Identification

Other Resources

Accessing ASP.NET Controls Programmatically