Loading ...

How to find all the textboxes in a page in c#

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  web forms / data controls   » How to find all the textboxes in a page in c#

How to find all the textboxes in a page in c#

Posts under the topic: How to find all the textboxes in a page in c#

Posted: 5/20/2010

Lurker 170  points  Lurker
  • Joined on: 10/17/2009
  • Posts: 34

Hi friends, 

I need to find all the textboxes in a page in c#. I need to disable one by one, how can I do it ?


Posted: 5/20/2010

Contributor 2255  points  Contributor
  • Joined on: 11/30/2008
  • Posts: 11
  Answered

shawn said:

Hi friends, 

I need to find all the textboxes in a page in c#. I need to disable one by one, how can I do it ?

 

Hi Shawn,

Try following code:

public static void DisableTextBoxes(Control parent)
{
foreach (Control c in parent.Controls)
{
if (c.GetType() == typeof(TextBox))
{
((TextBox)(c)).Enabled = false;

}
if (c.HasControls())
{
DisableTextBoxes(c);
}
}
}

To call the above method:

protected void Page_Load(object sender, EventArgs e)
{
DisableTextBoxes(Page);
}

Let me know if you have any problem to implement this.


tags Controls

Posted: 5/21/2010

Guru 16518  points  Guru
  • Joined on: 4/19/2009
  • Posts: 483
  Answered

In 2.0

<%@ 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>

 

 

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)
    {
        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>



tags c#
Page 1 of 1 (3 items)