Sideway
output.to from Sideway
Draft for Information Only

Content

How to: Locate Controls by ID
 Locate Controls by ID in an ASP.NET Web Page
  To locate a control that is not inside a naming container
   Reference the control's ID to access the object
  Examples
   Examples of ASP.NET C# page
   Examples of ASP.NET VB page
  To locate a control that is inside a naming container when you have a reference to the naming container
   Call the FindControl method of the naming container
  Locating a Control Inside a Hierarchy of Naming Containers
  To locate a control by searching through a hierarchy of naming containers
   Create a method that can be called recursively to search a naming control and its child naming containers
  Examples
   Examples of ASP.NET C# page
   Examples of ASP.NET VB page
 See Also
 Concepts
 Source and Reference

How to: Locate Controls by ID

Locate Controls by ID in an ASP.NET Web Page

 When a control is not inside a naming container, you can get a reference to it by using the control's ID. When a control is inside a naming container, you must call a method that searches the naming container for the control's ID. A control might also be inside a naming container that you do not have direct access to.

To locate a control that is not inside a naming container

 

Reference the control's ID to access the object

Reference the control's ID to access the object.
The following example includes code that shows how to access a control that is not inside a naming container. The Label control named Message is not within a naming container and therefore can be accessed by ID.

Examples

Examples of ASP.NET C# page

Examples of ASP.NET C# page
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#" %>
        <script runat="server">

            private class Record
            {
                public string ProductID { get; set; }
                public string Name { get; set; }
                public string ProductNumber { get; set; }
                public string DiscontinuedDate { get; set; }
            }

            void Page_Load()
            {
                Message.Text = String.Empty;

                var list = new List<Record>();

                list.Add(new Record()
                {
                    ProductID = "001",
                    Name = "Adjustable Race", 
                    ProductNumber = "AR-5381", 
                    DiscontinuedDate = "" });

                list.Add(new Record()
                {
                    ProductID = "002",
                    Name = "Bearing Ball",
                    ProductNumber = "BA-8327",
                    DiscontinuedDate = "1/6/2002"});

                list.Add(new Record()
                {
                    ProductID = "003",
                    Name = "BB Ball Bearing",
                    ProductNumber = "BE-2349",
                    DiscontinuedDate = "1/6/2002"});

                list.Add(new Record()
                {
                    ProductID = "001",
                    Name = "Headset Ball Bearings",
                    ProductNumber = "BE-2908",
                    DiscontinuedDate = "" });

                ProductsListView.DataSource = list;
                ProductsListView.DataBind();
            }

            void ProductsListView_SelectedIndexChanging(Object sender, ListViewSelectEventArgs e)
            {
                ListViewItem item = (ListViewItem)ProductsListView.Items[e.NewSelectedIndex];
                Label l = (Label)item.FindControl("DiscontinuedDateLabel");

                if (String.IsNullOrEmpty(l.Text))
                {
                    return;
                }

                DateTime discontinued = DateTime.Parse(l.Text);
                if (discontinued < DateTime.Now)
                {
                    Message.Text = "You cannot select a discontinued item.";
                    e.Cancel = true;
                }
            }

            protected void ProductsListView_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
            {
                // Clear selection.
                ProductsListView.SelectedIndex = -1;
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">

            <h3>ListView.SelectedIndexChanging Example</h3>

            <asp:Label ID="Message"
                ForeColor="Red"
                runat="server"/>
            <br/>

            <asp:ListView ID="ProductsListView"
                DataKeyNames="ProductID"
                OnSelectedIndexChanging="ProductsListView_SelectedIndexChanging"
                OnPagePropertiesChanging="ProductsListView_PagePropertiesChanging"
                runat="server" >
                <LayoutTemplate>
                    <table cellpadding="2" runat="server" id="tblProducts" width="640px">
                        <tr runat="server" id="itemPlaceholder" />
                    </table>

                    <asp:DataPager runat="server" ID="ProductsDataPager" PageSize="12">
                        <Fields>
                            <asp:NextPreviousPagerField
                                ShowFirstPageButton="true" ShowLastPageButton="true"
                                FirstPageText="|&lt;&lt; " LastPageText=" &gt;&gt;|"
                                NextPageText=" &gt; " PreviousPageText=" &lt; " />
                        </Fields>
                    </asp:DataPager>
                </LayoutTemplate>
                <ItemTemplate>
                    <tr runat="server">
                        <td valign="top">
                            <asp:LinkButton ID="SelectButton" runat="server" Text="..." CommandName="Select" />
                        </td>
                        <td valign="top">
                            <asp:Label ID="NameLabel" runat="server" Text='<%#Eval("Name") %>' />
                        </td>
                        <td valign="top">
                            <asp:Label ID="ProductNumberLabel" runat="server" Text='<%#Eval("ProductNumber") %>' />
                        </td>
                        <td>
                            <asp:Label ID="DiscontinuedDateLabel" runat="server" Text='<%#Eval("DiscontinuedDate", "{0:d}") %>' />
                        </td>
                    </tr>
                </ItemTemplate>
                <SelectedItemTemplate>
                    <tr runat="server" style="background-color:#ADD8E6">
                        <td>&nbsp;</td>
                        <td valign="top">
                            <asp:Label ID="NameLabel" runat="server" Text='<%#Eval("Name") %>' />
                        </td>
                        <td valign="top">
                            <asp:Label ID="ProductNumberLabel" runat="server" Text='<%#Eval("ProductNumber") %>' />
                        </td>
                        <td>
                            <asp:Label ID="DiscontinuedDateLabel" runat="server" Text='<%#Eval("DiscontinuedDate", "{0:d}") %>' />
                        </td>
                    </tr>
                </SelectedItemTemplate>
            </asp:ListView>

            <asp:SqlDataSource ID="ProductsDataSourcea" runat="server"
                ConnectionString="<%$ ConnectionStrings:AdventureworkConnection %>"
                SelectCommand="SELECT [ProductID], [Name], [ProductNumber], [DiscontinuedDate] 
                    FROM Production.Product"
                UpdateCommand="UPDATE Production.Product
                    SET Name = @Name, ProductNumber = @ProductNumber, DiscontinuedDate = @DiscontinuedDate
                    WHERE ProductID = @ProductID">
            </asp:SqlDataSource>

        </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_locatecontrolsbyid_001a_01.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="zjGO2z4GVTYFMveT8ogRcMH2XxMqvSZ0xO3uM31M1MCrlaz5dKmhS3hQlbSUqNXl9Iy9qCxywTiACixuBln9qJfi7y67k1SQq6JC67KFgLPY8UFWa9YA3hQwTpNzxVDVRzdh1bD5nsrSuaVvyjl0hTR61TiZssCBLZZQ3W6C07KKOVpzKVbAKfXWnN2QVLSnFspGCN4NPmQGDggTjfYCRNnJZuxlwPa213/+W4hdgnls2fAAhgF53FrkeTXMBwY229LD89vyjI1CUwVZjW4K/8cCDmT9g6APRGWQ01SKdd828mcavKbXj05jOgEFxNRjFbwDVtXn9dPrOp5CdcH7NTxbQfpZbfzQLBFX6VcalcadS8vNLy+xwPWdonxi2lZGb7hzRMIp7NZhScx4e2tZYNcSxczy1xVXOPJH/K7yukfUIcu7M1f/Mv7REuy4YW7kNrKW1+tiw6cjyFOlaBzUtxhiDoqTy6GPuFGRNkXnwnaHncscKRFSV62wVMnQ/w6D0lzBUgV5y4Qn6qPMpUEXx4Yy5sVLGSQBwRqhyyZU6m0K57kJUhr+n8c1UnAM+5+nHEPCv/36bLtY1BhfdRWwzZQM2oeS7S+q87pNoCuC8KiPWquRKmdKeMCaQbcc2TZGXtJi4+eldRchYxBe99Ozf8X5iMjN5UDNDPQtz5VM3iXIdB0jqnWKhjPtdR4/yRbbWiHZzgOeGRKkLvBSZqR52iuBXa0StAa2Xcy/a/iiJb5+w6yh1T6/4NF1bc2fu7eCqf6DY52s1eYDefFUYKgFWpOlnAx2Kc6YLDTe27VB8KQY2TtnYscxkfbTgEySkqxLp3VWEN741ElOZlweHp6Q8hL9W072RY4QeUoomgML3SBt/yIUncvMTwW5E0Jq4Ld9BvVeD39zSCDsTL/HBsoPQbkjN0/NOmMf/rEWYzoTSdI=" />
</div>

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
    theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>


<div class="aspNetHidden">

	<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="B149B4B4" />
	<input type="hidden" name="__VIEWSTATEENCRYPTED" id="__VIEWSTATEENCRYPTED" value="" />
	<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="Nd1U6g1LPqcmBQrrhrZzw73noOjXR1TsvX6lzGBLNTawWo5ItBYJ6s5tHVyS7a0gN8W7vbB45FCH+JzSl1n1clvyDuBfV9kBYm37ZqpcPN8zAm3nYBX3zJvU4xnW5q0eta+BkQsL3yc/bl52sB9RPMKeIL7CEnkeQ2iXm2XaO+/bZHxgCmWVMpRUh0Iv8DThQ9Jgwm3ut+MYe+TIvu+rtA==" />
</div>

            <h3>ListView.SelectedIndexChanging Example</h3>

            <span id="Message" style="color:Red;"></span>
            <br/>

                         <table id="ProductsListView_tblProducts" cellpadding="2" width="640px">

		
                    <tr>
			<td valign="top">
                            <a id="ProductsListView_SelectButton_0" href="javascript:__doPostBack(&#39;ProductsListView$ctrl0$SelectButton&#39;,&#39;&#39;)">...</a>
                        </td>
			<td valign="top">
                            <span id="ProductsListView_NameLabel_0">Adjustable Race</span>
                        </td>
			<td valign="top">
                            <span id="ProductsListView_ProductNumberLabel_0">AR-5381</span>
                        </td>
			<td>
                            <span id="ProductsListView_DiscontinuedDateLabel_0"></span>
                        </td>
		</tr>
		
         		
                    <tr>
			<td valign="top">
                            <a id="ProductsListView_SelectButton_1" href="javascript:__doPostBack(&#39;ProductsListView$ctrl1$SelectButton&#39;,&#39;&#39;)">...</a>
                        </td>
			<td valign="top">
                            <span id="ProductsListView_NameLabel_1">Bearing Ball</span>
                        </td>
			<td valign="top">
                            <span id="ProductsListView_ProductNumberLabel_1">BA-8327</span>
                        </td>
			<td>
                            <span id="ProductsListView_DiscontinuedDateLabel_1">1/6/2002</span>
                        </td>
		</tr>
		
         		
                    <tr>
			<td valign="top">
                            <a id="ProductsListView_SelectButton_2" href="javascript:__doPostBack(&#39;ProductsListView$ctrl2$SelectButton&#39;,&#39;&#39;)">...</a>
                        </td>
			<td valign="top">
                            <span id="ProductsListView_NameLabel_2">BB Ball Bearing</span>
                        </td>
			<td valign="top">
                            <span id="ProductsListView_ProductNumberLabel_2">BE-2349</span>
                        </td>
			<td>
                            <span id="ProductsListView_DiscontinuedDateLabel_2">1/6/2002</span>
                        </td>
		</tr>
		
         		
                    <tr>
			<td valign="top">
                            <a id="ProductsListView_SelectButton_3" href="javascript:__doPostBack(&#39;ProductsListView$ctrl3$SelectButton&#39;,&#39;&#39;)">...</a>
                        </td>
			<td valign="top">
                            <span id="ProductsListView_NameLabel_3">Headset Ball Bearings</span>
                        </td>
			<td valign="top">
                            <span id="ProductsListView_ProductNumberLabel_3">BE-2908</span>
                        </td>
			<td>
                            <span id="ProductsListView_DiscontinuedDateLabel_3"></span>
                        </td>
		</tr>
		
                </table>


                    <span id="ProductsListView_ProductsDataPager"><a class="aspNetDisabled">|<< </a>&nbsp;<a class="aspNetDisabled"> < </a>&nbsp;<a class="aspNetDisabled"> > </a>&nbsp;<a class="aspNetDisabled"> >>|</a>&nbsp;</span>
        </form>
    </body>
</html>
ASPX Web Page Embedded Output:

Examples of ASP.NET VB page

Examples of ASP.NET VB page
ASP.NET Code Input:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head runat="server">
        <title>Sample Page</title>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
        <%@ Page Language="vb" %>
        <script runat="server" >
            Private Class Record
                Public Property  ProductID
                Public Property Name
                Public Property ProductNumber 
                Public Property DiscontinuedDate 
            End Class

            Sub Page_Load()
                Message.Text = String.Empty

                Dim list As new List(Of Record)()

                list.Add(new Record() With { _
                    .ProductID = "001", _
                    .Name = "Adjustable Race", _
                    .ProductNumber = "AR-5381", _
                    .DiscontinuedDate = "" })

                list.Add(new Record() With { _
                    .ProductID = "002", _
                    .Name = "Bearing Ball", _
                    .ProductNumber = "BA-8327", _
                    .DiscontinuedDate = "1/6/2002"})

                list.Add(new Record() With { _
                    .ProductID = "003", _
                    .Name = "BB Ball Bearing", _
                    .ProductNumber = "BE-2349", _
                    .DiscontinuedDate = "1/6/2002"})

                list.Add(new Record() With { _
                    .ProductID = "001", _
                    .Name = "Headset Ball Bearings", _
                    .ProductNumber = "BE-2908", _
                    .DiscontinuedDate = "" })

                ProductsListView.DataSource = list
                ProductsListView.DataBind()
            End Sub

            Sub ProductsListView_SelectedIndexChanging(ByVal sender As Object, ByVal e As ListViewSelectEventArgs)
                Dim item As ListViewItem = CType(ProductsListView.Items(e.NewSelectedIndex), ListViewItem)
                Dim l As Label = CType(item.FindControl("DiscontinuedDateLabel"), Label)

                If String.IsNullOrEmpty(l.Text) Then
                    Return
                End If

                Dim discontinued As DateTime = DateTime.Parse(l.Text)
                If discontinued < DateTime.Now Then
                    Message.Text = "You cannot select a discontinued item."
                    e.Cancel = True
                End If
            End Sub

            Protected Sub ProductsListView_PagePropertiesChanging(ByVal sender As Object, _
                ByVal e As PagePropertiesChangingEventArgs)
                ' Clears the selection.
                ProductsListView.SelectedIndex = -1
            End Sub

        </script>

    </head>
    <body>
        <form id="form1" runat="server">

            <h3>ListView.SelectedIndexChanging Example</h3>

            <asp:Label ID="Message"
                ForeColor="Red"
                runat="server"/>
            <br/>

            <asp:ListView ID="ProductsListView"
                DataKeyNames="ProductID"
                OnSelectedIndexChanging="ProductsListView_SelectedIndexChanging"
                OnPagePropertiesChanging="ProductsListView_PagePropertiesChanging"
                runat="server" >
                <LayoutTemplate>
                    <table cellpadding="2" runat="server" id="tblProducts" width="640px">
                        <tr runat="server" id="itemPlaceholder" />
                    </table>

                    <asp:DataPager runat="server" ID="ProductsDataPager" PageSize="12">
                        <Fields>
                            <asp:NextPreviousPagerField
                                ShowFirstPageButton="true" ShowLastPageButton="true"
                                FirstPageText="|&lt;&lt; " LastPageText=" &gt;&gt;|"
                                NextPageText=" &gt; " PreviousPageText=" &lt; " />
                        </Fields>
                    </asp:DataPager>
                </LayoutTemplate>
                <ItemTemplate>
                    <tr runat="server">
                        <td valign="top">
                            <asp:LinkButton ID="SelectButton" runat="server" Text="..." CommandName="Select" />
                        </td>
                        <td valign="top">
                            <asp:Label ID="NameLabel" runat="server" Text='<%#Eval("Name") %>' />
                        </td>
                        <td valign="top">
                            <asp:Label ID="ProductNumberLabel" runat="server" Text='<%#Eval("ProductNumber") %>' />
                        </td>
                        <td>
                            <asp:Label ID="DiscontinuedDateLabel" runat="server" Text='<%#Eval("DiscontinuedDate", "{0:d}") %>' />
                        </td>
                    </tr>
                </ItemTemplate>
                <SelectedItemTemplate>
                    <tr runat="server" style="background-color:#ADD8E6">
                        <td>&nbsp;</td>
                        <td valign="top">
                            <asp:Label ID="NameLabel" runat="server" Text='<%#Eval("Name") %>' />
                        </td>
                        <td valign="top">
                            <asp:Label ID="ProductNumberLabel" runat="server" Text='<%#Eval("ProductNumber") %>' />
                        </td>
                        <td>
                            <asp:Label ID="DiscontinuedDateLabel" runat="server" Text='<%#Eval("DiscontinuedDate", "{0:d}") %>' />
                        </td>
                    </tr>
                </SelectedItemTemplate>
            </asp:ListView>

            <asp:SqlDataSource ID="ProductsDataSourcea" runat="server"
                ConnectionString="<%$ ConnectionStrings:AdventureworkConnection %>"
                SelectCommand="SELECT [ProductID], [Name], [ProductNumber], [DiscontinuedDate] 
                    FROM Production.Product"
                UpdateCommand="UPDATE Production.Product
                    SET Name = @Name, ProductNumber = @ProductNumber, DiscontinuedDate = @DiscontinuedDate
                    WHERE ProductID = @ProductID">
            </asp:SqlDataSource>

        </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><title>
	Sample Page
</title><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /></head>
    <body>
        <form method="post" action="./aspnetht_locatecontrolsbyid_001a_02.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="+TYHdAZ/qJTV4H2Ag4h0P6kZKc+T8HjRG1+HBmMUDthWHuo3ofCct2eXc4v8XDNinExwkc+JxEtmaRSKYfLwKbC8wg7v7+fOZ5sq/Kqn3UBanY9RYu5wwrrE6plvlj0R3HSg92P2hYZgNMM5CNpkeGQ+JnEv2YbEG5ar4eB5oylaP3lBBCYit0AGu3iu41CG8OKKhd38wVeNosHBNYwmno/DEM13sy0Tx7TnIdqAvHtQs5/9hsrm7/mpKNU2rGP1lY7FQMggNB0T5nGqCi/Z6jgGlWiPo76U/rHzVmk1KfXczyxrE9Kyirz4iqU2lyQELjkOVJ2molg+CfwZc4MKtztH2WKN0pZ4Re8kcuRe6vsAv9GPA2mO6RZ5YCWSDEmA0jop+chX3elBq+DbB7qK7nUeFuQBx0kxrRxYbm0T/Q5pO6T9XtISJ6th4FBPZ8OZ1mL4P81hUjasNRBm9GPeeiSSfy3uJRqJDAbHoiGRUTCHcsco9Z5i5RGHo9h+pw/EnKX5iVaZlExDXGBqBgIxvbS31/Oarn5pHt/PXkMqfXqBYiO+B5u8YNRWhwDQkGOEiMYM+dCFSRrlirfaKeFfAVxJ1DflQvDsdJrsH9SXlWwxnFrHjX48FMfWy1EM8/99a/9p/+9ZW8hLefe6Ax3X5jUynXkddR7Ga70f5od6VAPT58yQz8C0rLRe6cCOzpK334v7CBdaGOo1oJV0W23EHaCPrv2bE3fqh7SjKzAOj+/Q0yJnGcSTYzNsaKtP7HqiUnHwbTKK7m2BMjCiV3ak0T/EkRD/TwKVtaNrGVYCD5hm3pZS9HamEgicr2ivgtTXmaaI0jyI+C8xAEazF5ZshRHx0GICe5SpV63vPkSuwtniALLzN0kjR5mCL7PsfzNydiQJrH6IcSSuovEn2X0X/J493WAh3ZpTSSWxbnDftMU=" />
</div>

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
    theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>


<div class="aspNetHidden">

	<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="93134A16" />
	<input type="hidden" name="__VIEWSTATEENCRYPTED" id="__VIEWSTATEENCRYPTED" value="" />
	<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="ujqFObttWfASWeKJp8cGxQisrkifG2dC/IGIeicJKtGXCL6BR+AfYOjIm0E299yxpDYEoANwE7X+vQzz5px2weVXRznnnufe7/spkxEXUsLEi2uIIZRF3DWIMwkFK2oYPxT7OVXF0FRH4TOV8BoI2GZdwQ9zb346+MpLFzSs4zc/lIOGV9rf9zp/aAsTJeDYecIO5+QQN+hrP8LsHZKu1Q==" />
</div>

            <h3>ListView.SelectedIndexChanging Example</h3>

            <span id="Message" style="color:Red;"></span>
            <br/>

                         <table id="ProductsListView_tblProducts" cellpadding="2" width="640px">

		
                    <tr>
			<td valign="top">
                            <a id="ProductsListView_SelectButton_0" href="javascript:__doPostBack(&#39;ProductsListView$ctrl0$SelectButton&#39;,&#39;&#39;)">...</a>
                        </td>
			<td valign="top">
                            <span id="ProductsListView_NameLabel_0">Adjustable Race</span>
                        </td>
			<td valign="top">
                            <span id="ProductsListView_ProductNumberLabel_0">AR-5381</span>
                        </td>
			<td>
                            <span id="ProductsListView_DiscontinuedDateLabel_0"></span>
                        </td>
		</tr>
		
         		
                    <tr>
			<td valign="top">
                            <a id="ProductsListView_SelectButton_1" href="javascript:__doPostBack(&#39;ProductsListView$ctrl1$SelectButton&#39;,&#39;&#39;)">...</a>
                        </td>
			<td valign="top">
                            <span id="ProductsListView_NameLabel_1">Bearing Ball</span>
                        </td>
			<td valign="top">
                            <span id="ProductsListView_ProductNumberLabel_1">BA-8327</span>
                        </td>
			<td>
                            <span id="ProductsListView_DiscontinuedDateLabel_1">1/6/2002</span>
                        </td>
		</tr>
		
         		
                    <tr>
			<td valign="top">
                            <a id="ProductsListView_SelectButton_2" href="javascript:__doPostBack(&#39;ProductsListView$ctrl2$SelectButton&#39;,&#39;&#39;)">...</a>
                        </td>
			<td valign="top">
                            <span id="ProductsListView_NameLabel_2">BB Ball Bearing</span>
                        </td>
			<td valign="top">
                            <span id="ProductsListView_ProductNumberLabel_2">BE-2349</span>
                        </td>
			<td>
                            <span id="ProductsListView_DiscontinuedDateLabel_2">1/6/2002</span>
                        </td>
		</tr>
		
         		
                    <tr>
			<td valign="top">
                            <a id="ProductsListView_SelectButton_3" href="javascript:__doPostBack(&#39;ProductsListView$ctrl3$SelectButton&#39;,&#39;&#39;)">...</a>
                        </td>
			<td valign="top">
                            <span id="ProductsListView_NameLabel_3">Headset Ball Bearings</span>
                        </td>
			<td valign="top">
                            <span id="ProductsListView_ProductNumberLabel_3">BE-2908</span>
                        </td>
			<td>
                            <span id="ProductsListView_DiscontinuedDateLabel_3"></span>
                        </td>
		</tr>
		
                </table>


                    <span id="ProductsListView_ProductsDataPager"><a class="aspNetDisabled">|<< </a>&nbsp;<a class="aspNetDisabled"> < </a>&nbsp;<a class="aspNetDisabled"> > </a>&nbsp;<a class="aspNetDisabled"> >>|</a>&nbsp;</span>
        </form>
    </body>
</html>
ASPX Web Page Embedded Output:

To locate a control that is inside a naming container when you have a reference to the naming container

Call the FindControl method of the naming container

  Call the FindControl method of the naming container, passing a string that contains the ID of the control that you want to use. The method returns an object of type Control that you must cast to the appropriate type.
The example in the previous procedure also shows how you can access a control that is inside a naming container. The Label control named DiscontinuedDateLabel is inside a ListView control. Therefore, to access the Label control, you must call the FindControl method of the ListView control.

Locating a Control Inside a Hierarchy of Naming Containers

 Sometimes, a control is inside a naming container but you do not have a reference to the naming container. In that case, one way to get a reference to the control is to write a custom method that searches the controls in a hierarchy of naming containers.

To locate a control by searching through a hierarchy of naming containers

Create a method that can be called recursively to search a naming control and its child naming containers

  Create a method that can be called recursively to search a naming control and its child naming containers.
The following example shows one way to write a search method.

Examples

Examples of ASP.NET C# page

Examples of ASP.NET C# page
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#" %>
        <script runat="server">

            private class Record
            {
                public string ProductID { get; set; }
                public string Name { get; set; }
                public string ProductNumber { get; set; }
                public string DiscontinuedDate { get; set; }
            }

            void Page_Load()
            {
                var list = new List<Record>();

                list.Add(new Record()
                {
                    ProductID = "001",
                    Name = "Adjustable Race", 
                    ProductNumber = "AR-5381", 
                    DiscontinuedDate = "" });

                ProductsListView.DataSource = list;
                ProductsListView.DataBind();
            }

            
            private Control FindControlRecursive(Control rootControl, string controlID)
            {
                if (rootControl.ID == controlID) return rootControl;

                foreach (Control controlToSearch in rootControl.Controls)
                {
                    Control controlToReturn =
                        FindControlRecursive(controlToSearch, controlID);
                    if (controlToReturn != null) return controlToReturn;
                }
                return null;
            }
            
        </script>
    </head>
    <body>
        <form id="form1" runat="server">

            <h3>FindControlRecursive Example</h3>

            FindControlRecursive(Page,"ProductsDataPager"):<br />
            &nbsp; &emsp; "<%=FindControlRecursive(Page,"ProductsDataPager")%>"<br />
            FindControl(Page,"ProductsDataPager"):<br />
            &nbsp; &emsp; "<%=FindControl("ProductsDataPager")%>"<br />
            Page.FindControl("ProductsListView").FindControl("ProductsDataPager"):<br />
            &nbsp; &emsp; "<%=Page.FindControl("ProductsListView").FindControl("ProductsDataPager")%>"<br />

            <asp:ListView ID="ProductsListView"
                runat="server" >
                <LayoutTemplate>
                    <table cellpadding="2" runat="server" id="tblProducts" width="640px">
                        <tr runat="server" id="itemPlaceholder" />
                    </table>

                    <asp:DataPager runat="server" ID="ProductsDataPager" PageSize="12">
                        <Fields>
                            <asp:NextPreviousPagerField
                                NextPageText=" " PreviousPageText=" " />
                        </Fields>
                    </asp:DataPager>
                </LayoutTemplate>
                <ItemTemplate>
                </ItemTemplate>
            </asp:ListView>

        </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_locatecontrolsbyid_001a_03.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTcyNDc3ODQ4Mw9kFgICAw9kFgJmDxQrAAIPFgQeC18hRGF0YUJvdW5kZx4LXyFJdGVtQ291bnQCAWRkFgJmD2QWAgIDDxQrAAJkEBYAFgAWAGQYAgUiUHJvZHVjdHNMaXN0VmlldyRQcm9kdWN0c0RhdGFQYWdlcg8UKwAEZGQCDAIBZAUQUHJvZHVjdHNMaXN0Vmlldw8UKwAOZGRkZGRkZBQrAAFkAgFkZGRmAgxk5ashNgyw8bTCKRUqmDfHc6bBjVk6tsPKFhmepFdfx3M=" />
</div>

<div class="aspNetHidden">

	<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="7ABA55A0" />
</div>

            <h3>FindControlRecursive Example</h3>

            FindControlRecursive(Page,"ProductsDataPager"):<br />
            &nbsp; &emsp; "System.Web.UI.WebControls.DataPager"<br />
            FindControl(Page,"ProductsDataPager"):<br />
            &nbsp; &emsp; ""<br />
            Page.FindControl("ProductsListView").FindControl("ProductsDataPager"):<br />
            &nbsp; &emsp; "System.Web.UI.WebControls.DataPager"<br />

                         <table id="ProductsListView_tblProducts" cellpadding="2" width="640px">

		
                </table>


                    <span id="ProductsListView_ProductsDataPager"><a class="aspNetDisabled"> </a>&nbsp;<a class="aspNetDisabled"> </a>&nbsp;</span>
           </form>
    </body>
</html>
ASPX Web Page Embedded Output:

Examples of ASP.NET VB page

Examples of ASP.NET VB page
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" %>
        <script runat="server" >
            Private Class Record
                Public Property  ProductID
                Public Property Name
                Public Property ProductNumber 
                Public Property DiscontinuedDate 
            End Class

            Sub Page_Load()

                Dim list As new List(Of Record)()

                list.Add(new Record() With { _
                    .ProductID = "001", _
                    .Name = "Adjustable Race", _
                    .ProductNumber = "AR-5381", _
                    .DiscontinuedDate = "" })

                ProductsListView.DataSource = list
                ProductsListView.DataBind()
            End Sub

            Private Function FindControlRecursive(ByVal rootControl As Control, ByVal controlID As String) As Control

                If rootControl.ID = controlID Then
                    Return rootControl
                End If

                For Each controlToSearch As Control In rootControl.Controls
                    Dim controlToReturn As Control = FindControlRecursive(controlToSearch, controlID)
                    If controlToReturn IsNot Nothing Then
                        Return controlToReturn
                    End If
                Next
                Return Nothing
            End Function

        </script>

    </head>
    <body>
        <form id="form1" runat="server">

            <h3>FindControlRecursive Example</h3>

            FindControlRecursive(Page,"ProductsDataPager"):<br />
            &nbsp; &emsp; "<%=FindControlRecursive(Page,"ProductsDataPager")%>"<br />
            FindControl(Page,"ProductsDataPager"):<br />
            &nbsp; &emsp; "<%=FindControl("ProductsDataPager")%>"<br />
            Page.FindControl("ProductsListView").FindControl("ProductsDataPager"):<br />
            &nbsp; &emsp; "<%=Page.FindControl("ProductsListView").FindControl("ProductsDataPager")%>"<br />

            <asp:ListView ID="ProductsListView"
                runat="server" >
                <LayoutTemplate>
                    <table cellpadding="2" runat="server" id="tblProducts" width="640px">
                        <tr runat="server" id="itemPlaceholder" />
                    </table>

                    <asp:DataPager runat="server" ID="ProductsDataPager" PageSize="12">
                        <Fields>
                            <asp:NextPreviousPagerField
                                NextPageText=" " PreviousPageText=" " />
                        </Fields>
                    </asp:DataPager>
                </LayoutTemplate>
                <ItemTemplate>
                </ItemTemplate>
            </asp:ListView>

        </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_locatecontrolsbyid_001a_04.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTcyNDc3ODQ4Mw9kFgICAw9kFgJmDxQrAAIPFgQeC18hRGF0YUJvdW5kZx4LXyFJdGVtQ291bnQCAWRkFgJmD2QWAgIDDxQrAAJkEBYAFgAWAGQYAgUiUHJvZHVjdHNMaXN0VmlldyRQcm9kdWN0c0RhdGFQYWdlcg8UKwAEZGQCDAIBZAUQUHJvZHVjdHNMaXN0Vmlldw8UKwAOZGRkZGRkZBQrAAFkAgFkZGRmAgxk8JFOdXzJSHxLsKvksbjWq01vs+HuHuFBlsQ0nXUuRpc=" />
</div>

<div class="aspNetHidden">

	<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="12126942" />
</div>

            <h3>FindControlRecursive Example</h3>

            FindControlRecursive(Page,"ProductsDataPager"):<br />
            &nbsp; &emsp; "System.Web.UI.WebControls.DataPager"<br />
            FindControl(Page,"ProductsDataPager"):<br />
            &nbsp; &emsp; ""<br />
            Page.FindControl("ProductsListView").FindControl("ProductsDataPager"):<br />
            &nbsp; &emsp; "System.Web.UI.WebControls.DataPager"<br />

                         <table id="ProductsListView_tblProducts" cellpadding="2" width="640px">

		
                </table>


                    <span id="ProductsListView_ProductsDataPager"><a class="aspNetDisabled"> </a>&nbsp;<a class="aspNetDisabled"> </a>&nbsp;</span>
           </form>
    </body>
</html>
ASPX Web Page Embedded Output:
This method accepts a reference to a naming container. If you do not know which naming container on the page has the control that you are looking for, you can pass in the page itself as the top-level naming container. The method looks through all controls in the naming container that you pass to it. If it does not find the requested control, the method calls itself recursively for each lower-level naming container.

See Also

 

Concepts

 Web Forms Control Identification

Source and Reference

https://docs.microsoft.com/en-us/previous-versions/aspnet/y81z8326(v=vs.100)

©sideway

ID: 211000016 Last Updated: 10/16/2021 Revision: 0 Ref:

close

References

  1. Active Server Pages,  , http://msdn.microsoft.com/en-us/library/aa286483.aspx
  2. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929%28v=vs.90%29.aspx
  3. ASP Best Practices,  , http://technet.microsoft.com/en-us/library/cc939157.aspx
  4. ASP Built-in Objects,  , http://msdn.microsoft.com/en-us/library/ie/ms524716(v=vs.90).aspx
  5. Response Object,  , http://msdn.microsoft.com/en-us/library/ms525405(v=vs.90).aspx
  6. Request Object,  , http://msdn.microsoft.com/en-us/library/ms524948(v=vs.90).aspx
  7. Server Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525541(v=vs.90).aspx
  8. Application Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525360(v=vs.90).aspx
  9. Session Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms524319(8v=vs.90).aspx
  10. ASPError Object,  , http://msdn.microsoft.com/en-us/library/ms524942(v=vs.90).aspx
  11. ObjectContext Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525667(v=vs.90).aspx
  12. Debugging Global.asa Files,  , http://msdn.microsoft.com/en-us/library/aa291249(v=vs.71).aspx
  13. How to: Debug Global.asa files,  , http://msdn.microsoft.com/en-us/library/ms241868(v=vs.80).aspx
  14. Calling COM Components from ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524620(v=VS.90).aspx
  15. IIS ASP Scripting Reference,  , http://msdn.microsoft.com/en-us/library/ms524664(v=vs.90).aspx
  16. ASP Keywords,  , http://msdn.microsoft.com/en-us/library/ms524672(v=vs.90).aspx
  17. Creating Simple ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524741(v=vs.90).aspx
  18. Including Files in ASP Applications,  , http://msdn.microsoft.com/en-us/library/ms524876(v=vs.90).aspx
  19. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929(v=vs.90).aspx
  20. FileSystemObject Object,  , http://msdn.microsoft.com/en-us/library/z9ty6h50(v=vs.84).aspx
  21. http://msdn.microsoft.com/en-us/library/windows/desktop/ms675944(v=vs.85).aspx,  , ADO Object Model
  22. ADO Fundamentals,  , http://msdn.microsoft.com/en-us/library/windows/desktop/ms680928(v=vs.85).aspx
close

Latest Updated LinksValid XHTML 1.0 Transitional Valid CSS!Nu Html Checker Firefox53 Chromena IExplorerna
IMAGE

Home 5

Business

Management

HBR 3

Information

Recreation

Hobbies 8

Culture

Chinese 1097

English 339

Reference 79

Computer

Hardware 249

Software

Application 213

Digitization 32

Latex 52

Manim 205

KB 1

Numeric 19

Programming

Web 289

Unicode 504

HTML 66

CSS 65

SVG 46

ASP.NET 270

OS 429

DeskTop 7

Python 72

Knowledge

Mathematics

Formulas 8

Algebra 84

Number Theory 206

Trigonometry 31

Geometry 34

Coordinate Geometry 2

Calculus 67

Complex Analysis 21

Engineering

Tables 8

Mechanical

Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5

Control

Process Control 1

Acoustics 19

FiniteElement 2

Natural Sciences

Matter 1

Electric 27

Biology 1

Geography 1


Copyright © 2000-2024 Sideway . All rights reserved Disclaimers last modified on 06 September 2019