How to make all textboxs text empty in a page

Snaini
Posted by Snaini under C# category on | Points: 40 | Views : 270
protected void btnSubmit_Click(object sender, EventArgs e)
{
/*................................
//here you can perform Insert action after that ,if we want cleat the text boxes text,for that i created one method "Clear(); "


//here this represents current entire form it Contains all controls

Example:
btnSubmit: {Text = "Save"}
form1 : {System.Web.UI.HtmlControls.HtmlForm }
txtAddress: {System.Web.UI.WebControls.TextBox}
txtCity: {System.Web.UI.WebControls.TextBox}
txtFirstName: {System.Web.UI.WebControls.TextBox}
txtLastName: {System.Web.UI.WebControls.TextBox}
txtPhone: {System.Web.UI.WebControls.TextBox}
...........................*/


Clear(this); //this is the method (calling method) here we are passing all the controls

}



//here in Controls we are getting All Controls
//Example: LiteralControls,HtmlHead,HtmlForm
//our controls resides at HtmlForm
//again in HtmlForm Controls also LiteralControls and Controls like textbox,button etc..
//so i am using two for loops and checking is it textbox or not
//System.Web.UI name space for Control
public void Clear(Control Controls ) //called method
{
foreach (Control childCobtrols in Controls.Controls)
{
foreach (Control childcontrol in childCobtrols.Controls)
{
if (childcontrol is TextBox)
{
//here i am doing type casting
((TextBox)childcontrol).Text = string.Empty;
}
}
}
}

Comments or Responses

Login to post response