How to Clear all textbox field in one form...?

Mittaikariss
Posted by Mittaikariss under C# category on | Points: 40 | Views : 6552
mostly we are using in asp.net page redirect same page or using null values in each textbox.text...

now i am using simple way ....get all textboxes in single textbox array and run the loop for example coding is.....

public void empty()
{
TextBox[] textempty = { txtcategoryname, txtshortcode, txtdescription };
for (int i = 0; i < textempty.Length; i++)
{
textempty[i].Text = string.Empty;
}
}

Invom Technology
http://www.invomtech.com/

Comments or Responses

Posted by: Ndebata on: 9/22/2011 Level:Starter | Status: [Member] | Points: 10
Hi Still You have to write a collection of all your text box, Imagine you have some data entry form which has around 50 text boxes then ?
public void BlankAllTextBoxControls(ControlCollection col)

{
foreach (Control item in col)
{
if(item==null) return;
if (item.HasControls()) BlankAllTextBoxControls(item.Controls);
else if (item is TextBox) ((TextBox)item).Text = string.Empty;
}
}


and just call this method for the container to reset all the textbox value.
In case you have a panel and you want to reset
 BlankAllTextBoxControls(Panel1.Controls);

For the whole page it can be extended
 BlankAllTextBoxControls(Page.Controls);

Posted by: Mittaikariss on: 9/22/2011 Level:Bronze | Status: [Member] | Points: 10
ya you are correct.....anyway ....thanks.bro......
Posted by: Avdesh on: 9/22/2011 Level:Starter | Status: [Member] | Points: 10
I CAN PROVIDE IT IN A MORE SIMPLE WAY

public void clearAlltextbox(Form fr)
{
foreach(control c in fr.controls)
{
if(c.gettype()==typeof(textbox))
{
((textbox)c).clear();
}
}
}

Posted by: CS1401 on: 9/23/2011 Level:Starter | Status: [Member] | Points: 10
Posted by: Gausi on: 9/30/2011 Level:Starter | Status: [Member] | Points: 10
you can use client side script [jQuery] to clear all textboxes in a form..

ex:-

 $('input[type="text"]').each(function() { this.value = ''; });


$('textarea').each(function() { this.value = ''; });

Login to post response