Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
How to clear a Multiple TextBox, Radio Button, Check Box values in a single click Generic in Asp.NET and C#
Posted
Comments
Loganathan.M 1-Oct-14 6:18am    
Thank u , But I ask Generic Format in Asp.NET and C#

I have this method and i call this method on a button click event to clear the forms data

Here is my method
public static void ClearFields(ControlCollection pageControls)
    {
        foreach (Control contl in pageControls)
        {
            string strCntName = (contl.GetType()).Name;

            switch (strCntName)
            {
                case "TextBox":
                    TextBox tbSource = (TextBox)contl;
                    tbSource.Text = "";
                    break;
                case "RadioButtonList":
                    RadioButtonList rblSource = (RadioButtonList)contl;
                    rblSource.SelectedIndex = -1;
                    break;
                case "DropDownList":
                    DropDownList ddlSource = (DropDownList)contl;
                    ddlSource.SelectedIndex = -1;
                    break;
                case "ListBox":
                    ListBox lbsource = (ListBox)contl;
                    lbsource.SelectedIndex = -1;
                    break;
            }
            ClearFields(contl.Controls);
        }
    }



Call this method on a button click such as follows\
C#
protected void btn_cancel_Click(object sender, EventArgs e)
    {
        ClearFields(Form.Controls);
    }
 
Share this answer
 
Comments
Loganathan.M 1-Oct-14 6:18am    
Thank u , But I ask Generic Format in Asp.NET and C#
link[]

link[]

refer these links
 
Share this answer
 
Hi
If you want to clear certain controls value then go for Client Side Scripts.
I have given a dummy scenario which matches your requirements.

<asp:CheckBox ID="CheckBox1" Text="Check Item" Checked="true"  runat="server" />
<asp:RadioButton ID="RadioButton1" Text="Radio Item" Checked="true" runat="server" />
<asp:TextBox ID="TextBox1"   runat="server" TextMode="MultiLine"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="ClearAll()" />


and the script is

XML
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
    function ClearAll() {
        $('#<%= TextBox1.ClientID %>').val("");
        $('#<%=CheckBox1.ClientID %>').prop("checked", false);
        $('#<%=RadioButton1.ClientID %>').prop("checked", false);

    }
</script>
 
Share this answer
 
Comments
Loganathan.M 1-Oct-14 6:18am    
Thank u , But I ask Generic Format in Asp.NET and C#
Ajith K Gatty 1-Oct-14 10:51am    
Hehe.. Sorry :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900