Click here to Skip to main content
15,072,627 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi all!!

I want to clear all the values from the textbox on my page. I used the following code. neither it's working nor it's giving me any error.


clear(Page);


private void clear( Control c)
   {
       foreach ( Control ctrl in c.Controls )
       {
           if (ctrl is TextBox)
           {
               TextBox tb = ctrl as TextBox;
               tb.Text = "";
           }
           else
           if (ctrl is DropDownList)
           {
               DropDownList ddl = ctrl as DropDownList;
               ddl.SelectedIndex=0;
           }
           else
               if (ctrl is CheckBox)
               {
                   CheckBox chk = ctrl as CheckBox;
                   chk.Checked = false;
               }

       }
   }
Posted
Comments
Reiss 30-Nov-11 10:26am
   
Are you sure your top level control c is what you think it is, when using master pages, user controls etc the control collections get messy.

Also the formatting on your else if blocks is really nasty - put the if on the same line as the else
ujjwal uniyal 30-Nov-11 10:36am
   
i am not using master page and there is no other c defined in my page. i was just trying to figure out some method to clear the fields.
Uday P.Singh 30-Nov-11 10:32am
   
where you have been calling this method in your code? post that also.
ujjwal uniyal 30-Nov-11 10:37am
   
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{

SqlConnection con;


protected void Page_Load(object sender, EventArgs e)
{


con = new SqlConnection(ConfigurationManager.ConnectionStrings["Constr"].ConnectionString);
con.Close();
databind();

}

private void clear( Control c)
{
foreach ( Control ctrl in c.Controls )
{
if (ctrl is TextBox)
{
TextBox tb = ctrl as TextBox;
tb.Text = "";
}
else
if (ctrl is DropDownList)
{
DropDownList ddl = ctrl as DropDownList;
ddl.SelectedIndex=0;
}
else
if (ctrl is CheckBox)
{
CheckBox chk = ctrl as CheckBox;
chk.Checked = false;
}

}
}


private void databind()
{
con.Open();
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection = con;
cmd1.CommandType = CommandType.Text;
cmd1.CommandText = "Select * from Person";
SqlDataAdapter da= new SqlDataAdapter();
da.SelectCommand = cmd1;
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();

}



protected void btnSubmitt_Click1(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Sp_Person_Insert";
cmd.Connection = con;
cmd.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = txtName.Text;
cmd.Parameters.Add("@Age", SqlDbType.Int).Value = txtAge.Text;
cmd.Parameters.Add("@Occupation", SqlDbType.VarChar, 50).Value = txtOccupation.Text;
cmd.Parameters.Add("@Company", SqlDbType.VarChar, 50).Value = txtCompany.Text;
cmd.ExecuteNonQuery();
con.Close();
clear(Page);
}
}

1 solution

Try this code:

C#
private void Clear(Control parent)
       {
           foreach (Control c in parent.Controls)
           {
               if (c.Controls.Count > 0)
               {
                   Clear(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 it like this:

C#
Clear(this);


hope it helps :)
   
v2
Comments
ujjwal uniyal 30-Nov-11 11:45am
   
hey what is this ResetFormControlValues ???? i tried the code without it. it didn't worked out for me.

i'll be needing the defination on ResetFormControlValues.
Uday P.Singh 30-Nov-11 12:43pm
   
I have updated the answer, have a look at it.
ujjwal uniyal 1-Dec-11 0:32am
   
thanks for the help sir. it worked with your previous answer too . I called clear(Form1) instead of clear(page). But i wonder how will i call it on content page ???

Will this updated code work on content page ??? Guess i have to see.

anyways thanks. :)
navarie2 30-Nov-11 12:04pm
   
try Clear instead of ResetFormControlValues. it will be recursive. so all textbox 's text will be cleared.
Uday P.Singh 30-Nov-11 12:43pm
   
exactly :)

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