Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Friday, 20 March 2015

While working in asp.net we need to clear large no of fields i.e. controls on submit or clear button. If there are small no of fields then we can clear these fields individually from server side. But in case of large no of fields it is not possible to clear fields like this.
In this article I have explained how we can clear all type of controls easily.

Source Code:
Design Section:
<fieldset style="width:300px">
    <legend><strong>Reset All type of controls </strong></legend>
        <table>
        <tr><td></td><td></td></tr>
            <tr><td></td><td></td></tr>
         <tr><td>TextBox</td><td>
             <asp:TextBox ID="lastname" runat="server"></asp:TextBox></td></tr>          
                <tr><td></td><td></td></tr>
                   <tr><td>Check Box list</td><td>         
                       <asp:CheckBoxList ID="CheckBoxList1" runat="server"
                           RepeatDirection="Horizontal" Height="24px" >
                           <asp:ListItem>Male</asp:ListItem>
                           <asp:ListItem>Female</asp:ListItem>
                       </asp:CheckBoxList>        
        </td></tr>
        <tr><td></td><td></td></tr>  
        <tr><td>Radio Button List</td><td>        

            <asp:RadioButton ID="rblnet" runat="server" Text="Asp.net" GroupName="a"/>
            <asp:RadioButton ID="rblphp"
                runat="server" Text="Php" GroupName="a" />        
        </td></tr>
        <tr><td></td><td></td></tr>
         <tr><td>DropDown List</td><td>
             <asp:DropDownList ID="ddlcity" runat="server">
             <asp:ListItem>--Select--</asp:ListItem>
                 <asp:ListItem>Delhi</asp:ListItem>
                 <asp:ListItem>Kolkata</asp:ListItem>
                 <asp:ListItem>Mumbai</asp:ListItem>
             </asp:DropDownList>
         </td></tr>
        <tr><td></td><td></td></tr>
            <tr><td></td><td></td></tr>
         <tr><td>Check Box </td><td>
             <asp:CheckBox ID="chk" runat="server" /></td></tr>      
         <tr><td>&nbsp;</td><td>
             &nbsp;</td></tr>      
              <tr><td></td><td>
                  <asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" />
                  </td></tr>
        </table>
     </fieldset>

ASP.NET Code Behind File Code:
   protected void Button1_Click(object sender, EventArgs e)
    {
        ClearControls();
    }
    public void ClearControls()
    {
        foreach (Control cntrl in form1.Controls)
        {
            if (cntrl.GetType() == typeof(TextBox))
            {
                ((TextBox)(cntrl)).Text = "";
            }
         
            else if (cntrl.GetType() == typeof(DropDownList))
            {
                ((DropDownList)(cntrl)).SelectedIndex = 0;
            }
            else if (cntrl.GetType() == typeof(CheckBox))
            {
                ((CheckBox)(cntrl)).Checked = false;
            }
            else if (cntrl.GetType() == typeof(CheckBoxList))
            {
                ((CheckBoxList)(cntrl)).ClearSelection();
            }
            else if (cntrl.GetType() == typeof(RadioButton))
            {
                ((RadioButton)(cntrl)).Checked = false;
            }
            else if (cntrl.GetType() == typeof(RadioButtonList))
            {
                ((RadioButtonList)(cntrl)).ClearSelection();
            }
        }
    }

Vb.Net Code:

Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        ClearControls()
    End Sub

    Public Sub ClearControls()
        For Each cntrl As Control In form1.Controls
            If cntrl.[GetType]() = GetType(TextBox) Then
                DirectCast(cntrl, TextBox).Text = ""
            ElseIf cntrl.[GetType]() = GetType(DropDownList) Then
                DirectCast(cntrl, DropDownList).SelectedIndex = 0
            ElseIf cntrl.[GetType]() = GetType(CheckBox) Then
                DirectCast(cntrl, CheckBox).Checked = False
            ElseIf cntrl.[GetType]() = GetType(CheckBoxList) Then
                DirectCast(cntrl, CheckBoxList).ClearSelection()
            ElseIf cntrl.[GetType]() = GetType(RadioButton) Then
                DirectCast(cntrl, RadioButton).Checked = False
            ElseIf cntrl.[GetType]() = GetType(RadioButtonList) Then
                DirectCast(cntrl, RadioButtonList).ClearSelection()
            End If
        Next


    End Sub
Categories: ,

0 comments:

Post a Comment