Access all controls from Page Class, UpdatePanel in ASP.NET

 

Sometimes we might have requirement to access all controls from Asp.Net page. By using Page Class or UpdatePanel Controls property we can access all controls within it.

 

If you want to clear the all controls within the UpdatePanel or if you want to assign value to particular type of controls we can do by using its ContentTemplateContainer.Controls property as shown below.

 

Default.aspx 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!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>Get Controls Collection based on Parent Control</title>

 

</head>

 

<body>

 

    <form id="form1" runat="server">

 

    <div>

 

        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

 

        <asp:UpdatePanel ID="UPanel1" runat="server">

 

            <ContentTemplate>

 

                <asp:TextBox ID="txt1" runat="server"></asp:TextBox><br />

 

                <asp:TextBox ID="txt2" runat="server"></asp:TextBox><br />

 

                <asp:Label ID="lbl1" runat="server"></asp:Label><br />

 

                <asp:Label ID="lbl2" runat="server"></asp:Label><br />

 

            </ContentTemplate>

 

        </asp:UpdatePanel>

 

    </div>

 

    </form>

 

</body>

 

</html>

 

Default.aspx.cs 

using System; 

using System.Web.UI; 

using System.Web.UI.WebControls;

 

public partial class _Default : System.Web.UI.Page 

{ 

    protected void Page_Load(object sender, EventArgs e) 

    {

 

        if (!IsPostBack) 

            GetChildControls(); 

    }

 

    private void GetChildControls() 

    { 

        //we are displaying the Page controls 

        foreach (Control cntrl in this.Page.Controls) 

            if (cntrl.ID != null) 

                Response.Write(cntrl.ID.ToString() + "<br/>"); 

 

        foreach (Control cntrl in UPanel1.ContentTemplateContainer.Controls) 

        { 

            if (cntrl is TextBox) 

            { 

                (cntrl as TextBox).Text = "This is Textbox"; 

            } 

            else if (cntrl is Label) 

            { 

                (cntrl as Label).Text = "This is Label"; 

            } 

            else if (cntrl is Literal) 

            { 

                (cntrl as Literal).Text = "This is Literal"; 

            } 

            else if (cntrl is GridView) 

            { 

                //this is Gridview control 

            } 

        } 

    } 

}

 

As shown above we are accessing the all controls within the UpdatePanel UPanel1 control by using ContentTemplateContainer.Controls property and we are assigning the some value for all TextBox controls and assigning some other value for all Label controls.

                                                                                                             AspNetGetControls.zip (3.19 kb)