Friday, 5 July 2013

how to clear All Controls

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:DropDow

No comments:

Post a Comment