Loading ...

ASP.NET: How to find all textboxes in a page | CodeAsp.Net

ASP.NET: How to find all textboxes in a page

 /5
0 (0votes)

In this blog I will show you how to find all the textboxes in a page in ASP.NET.  To show it I have posted a sample code below in which I have used few textboxes, labels, a dropdownlist. On page load we will find all the textboxes on this page and will disable them, why disable ? just to perform some operation on textboxes so that we can verify that all the textboxes has been covered. Here is the source code:

For 2.0 and above versions

 

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        DisableAllTextBoxes(Page);

    }
    private static void DisableAllTextBoxes(Control parent)
    {
        foreach (Control c in parent.Controls)
        {
            var tb = c as TextBox;
            if (tb != null)
            {
                tb.Enabled = false;
            }
            DisableAllTextBoxes(c);
        }

    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:TextBox ID="txtTest" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="lblTest" runat="server" Text="Test"></asp:Label>
    <asp:TextBox ID="txtTest2" runat="server"></asp:TextBox>
    <asp:DropDownList runat="server">
    </asp:DropDownList>
    </form>
</body>
</html>

 

 

 

For 3.5

 

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        DisableAllTextBoxes(Page);

    }
    private static void DisableAllTextBoxes(Control parent)
    {
        foreach (Control c in parent.Controls)
        {
            if (c is TextBox)
            {
                var tb = c as TextBox;
                tb.Enabled = false;
            }
            DisableAllTextBoxes(c);
        }

    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:TextBox ID="txtTest" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="lblTest" runat="server" Text="Test"></asp:Label>
    <asp:TextBox ID="txtTest2" runat="server"></asp:TextBox>
    <asp:DropDownList runat="server">
    </asp:DropDownList>
    </form>
</body>
</html>

 

One more way in 3.5

 

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        DisableAllTextBoxes(Page);

    }
    private static void DisableAllTextBoxes(Control parent)
    {
        if (parent != null)
            foreach (Control c in parent.Controls)
            {
                if (c is TextBox)
                {
                    using (var tb = c as TextBox)
                    {
                        tb.Enabled = false;
                    }
                }
                DisableAllTextBoxes(c);
            }
    }

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:TextBox ID="txtTest" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="lblTest" runat="server" Text="Test"></asp:Label>
    <asp:TextBox ID="txtTest2" runat="server"></asp:TextBox>
    <asp:DropDownList runat="server">
    </asp:DropDownList>
    </form>
</body>
</html>



 

In the above code what I am doing is I am disabling all the textboxes with this method:

 

    private static void DisableAllTextBoxes(Control parent)
    {
        foreach (Control c in parent.Controls)
        {
            var tb = c as TextBox;
            if (tb != null)
            {
                tb.Enabled = false;
            }
            DisableAllTextBoxes(c);
        }

    }

 


i.e I am passing "Page" as the parent control and then recursively traversing through each of the controls so that we can find all the textboxes and then apply some operation on it.

 

 

Do let me know your feedback comments

Comments (no comments yet)

Top Posts