Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
hi,
can i get the coding to clear multiple no of textboxes
if they are plentiful
Posted

Try this:

make a method like this:

C#
private void ClearFormControlValues(Control parent)
        {
            foreach (Control c in parent.Controls)
            {
                if (c.Controls.Count > 0)
                {
                    ResetFormControlValues(c);
                }
                else
                {
                    switch (c.GetType().ToString())
                    {
                        case "System.Web.UI.WebControls.TextBox":
                            ((TextBox)c).Text = "";
                            break;
                        case "System.Web.UI.WebControls.CheckBox":
                            ((CheckBox)c).Checked = false;
                            break;
                        case "System.Web.UI.WebControls.RadioButton":
                            ((RadioButton)c).Checked = false;
                            break;

                    }
                }
            }
        }


And call this method on button click or any other event you want like this:

C#
protected void submit_Click(object sender, EventArgs e)
{
     ClearFormControlValues(this);
}


this will clear all the textboxes, checkboxes & radio buttons on your form, you can remove checkbox and radiobutton if you want only textboxes to clear, in the same way you can add more control.

hope it helps :)
 
Share this answer
 
Comments
Uday P.Singh 13-Nov-11 12:06pm    
How can some one downvote without giving a reason?
Try this,

<input style="cursor: hand; width:80px;" title="reset all fields" type="button" value="Reset"  önclick="javascript:reset()" id="btReset" name="btReset" />


This will clear all the fields on the page. No need to count how many.
 
Share this answer
 
v4
Comments
RaviRanjanKr 13-Nov-11 2:15am    
Always wrap your code in "pre" tag.
Get the type of controls in your form, and if they are textboxes, clear them.

C#
foreach (TextBox txt in this.Controls.Find("textBox",true))
 {
    txt.Clear();
 }
 
Share this answer
 
textbox1.Text="";
textbox1.Text=null;
 
Share this answer
 
v2

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