How to clear states of all types of controls in asp.net form like textboxes, dropdownlists and checkboxes


Introduction:

Sometimes we have forms with 30 or 40 controls(includes textboxes , checkboxes and dropdownlists). At this time, if we want to clear all contols for single button click , normally we need to write code for all controls text to string.empty or null this is lengthy and timetaking process. to overcome this process, here we are creating following process

for this we need to place all controls in form tag only

if you are not interested to place in form

you can use any container controls like pannel or etc…

Using Form Tag:

Code in .aspx Page:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Cleartb.aspx.cs" Inherits="Cleartb" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:DropDownList ID="DropDownList1"
runat="server">
<asp:ListItem>default</asp:ListItem>
<asp:ListItem>first</asp:ListItem>
<asp:ListItem>second</asp:ListItem>
<asp:ListItem>third</asp:ListItem>
<asp:ListItem>fourth</asp:ListItem>
</asp:DropDownList>
<br />
<asp:CheckBox ID="CheckBox1" runat="server" />
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Clear" onclick="Button1_Click" />
</div>
</form>
</body>
</html>

Code Under Buttonclick event:
protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (Control ctrl in form1.Controls)
        {
            if (ctrl is TextBox)
            {
                TextBox tb = (TextBox)ctrl;
                tb.Text = string.Empty;
            }
            else if (ctrl is DropDownList)
            {
                DropDownList dl = (DropDownList)ctrl;
                dl.SelectedIndex = 0;
            }
            else if (ctrl is CheckBox)
            {
                CheckBox cb = (CheckBox)ctrl;
                cb.Checked = false;
            }
        }
    }

Using Panel Container:

Code in .aspx Page:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Cleartb.aspx.cs" Inherits="Cleartb" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">

<asp:Panel ID="Panel1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:DropDownList ID="DropDownList1"
runat="server">
<asp:ListItem>default</asp:ListItem>
<asp:ListItem>first</asp:ListItem>
<asp:ListItem>second</asp:ListItem>
<asp:ListItem>third</asp:ListItem>
<asp:ListItem>fourth</asp:ListItem>
</asp:DropDownList>
<br />
<asp:CheckBox ID="CheckBox1" runat="server" />
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Clear" onclick="Button1_Click" />
</div>

</asp:Panel>
</form>
</body>
</html>

Code Under Buttonclick event:
protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (Control ctrl in Panel1.Controls)
        {
            if (ctrl is TextBox)
            {
                TextBox tb = (TextBox)ctrl;
                tb.Text = string.Empty;
            }
            else if (ctrl is DropDownList)
            {
                DropDownList dl = (DropDownList)ctrl;
                dl.SelectedIndex = 0;
            }
            else if (ctrl is CheckBox)
            {
                CheckBox cb = (CheckBox)ctrl;
                cb.Checked = false;
            }
        }
    }

Using Javascript:

<html xmlns="http://www.w3.org/1999/xhtml&quot; >
<head id="Head1" runat="server">
<title>Reset controls</title>
<script language="javascript" type='text/javascript'>
function Clear() {
for (i = 0; i < document.forms[0].length; i++) {
doc = document.forms[0].elements[i];
switch (doc.type) {
case "text":
doc.value = "";
break;
case "checkbox":
doc.checked = false;
break;
case "radio":
doc.checked = false;
break;
case "select-one":
doc.options[doc.selectedIndex].selected = false;
break;
case "select-multiple":
while (doc.selectedIndex != -1) {
indx = doc.selectedIndex;
doc.options[indx].selected = false;
}
doc.selected = false;
break;

default:
break;
}
}
}

</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBox ID="CheckBox1"
runat="server" /><asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Default</asp:ListItem>
<asp:ListItem>First</asp:ListItem>
<asp:ListItem>Second</asp:ListItem>
<asp:ListItem>Third</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:TextBox ID="TextBox2"
runat="server"></asp:TextBox>
<input id="Button1" type='button' onclick='Clear()' value='ClearState'/>
</div>
</form>
</body>
</html>

Advertisements

3 thoughts on “How to clear states of all types of controls in asp.net form like textboxes, dropdownlists and checkboxes

  1. The simplest is to find all control under form type using javascript and then clear them accordingly or set the default value of them.This will require no post back either

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s