Empty the Multiple Text-Box control using single event in c#

CS1401
Posted by CS1401 under C# category on | Points: 40 | Views : 4346

protected void Button1_Click(object sender, EventArgs e)
{
ClearInputss(Page.Controls);
}

public void ClearInputss(ControlCollection ctrls)
{
foreach (Control ctrl in ctrls)
{
if (ctrl is TextBox)
((TextBox)ctrl).Text = string.Empty;
ClearInputss(ctrl.Controls);
}
}

Comments or Responses

Posted by: Karaniscool on: 7/19/2011 Level:Starter | Status: [Member] | Points: 10
A better way to do this kind of thing is on the client side. You can write a small piece of code in jquery.
Posted by: CS1401 on: 7/21/2011 Level:Starter | Status: [Member] | Points: 10
THanks
Posted by: Yogendra_svim on: 7/30/2011 Level:Starter | Status: [Member] | Points: 10
i tried this code but it is not running it gives error "c does not have property text"
Posted by: CS1401 on: 8/1/2011 Level:Starter | Status: [Member] | Points: 10
yoge that code working perfectly.. Do you have textbox in your form?
if(c is textbox) means first checking is this one is textbox or not?
afterwards only do the empty operation. try one more time.. If you cant give me your mail id.. i will give u a full code.
Posted by: Karaniscool on: 8/1/2011 Level:Starter | Status: [Member] | Points: 10
You can make this generic method as following on the client side. We should do these kind of small features on the client side with the help of javascript or jquery. You can call this js method from the onclick event of the button or link.

 function ClearText() {

var tbl = document.getElementById("tbl100");
var inputs = tbl.getElementsByTagName('input');
for (i = 0; i < inputs.length; i++) {
if (inputs[i].type == 'text') {
inputs[i].value = "";
}
}
}

Posted by: CS1401 on: 8/2/2011 Level:Starter | Status: [Member] | Points: 10
ok brother karaniscool how u implement this code in winform???!!
Posted by: Karaniscool on: 8/2/2011 Level:Starter | Status: [Member] | Points: 10

This will only work for the web apps but not for the winforms.
Posted by: CS1401 on: 8/4/2011 Level:Starter | Status: [Member] | Points: 10
ok karan. then how is my code.. Is it working in both(web & win)?
Posted by: Karaniscool on: 8/4/2011 Level:Starter | Status: [Member] | Points: 10

For the web application, it is better to perform simple form manipulations on the client side with the help of javascript. Thus preventing the postback and make the site easier to use.

For win app, the server-side code is fine.
Posted by: CS1401 on: 9/17/2011 Level:Starter | Status: [Member] | Points: 10
@Yogendra_svim: Here i updated my code.. now you can clear the value from all the textbox control using just one click.
thanks.
Posted by: Yogendra_svim on: 9/17/2011 Level:Starter | Status: [Member] | Points: 10
pls help to give me code for blankiing all textbox controle in a page in asp.net that page is attached with master page .
i have traied the given code but i am getting problem on it.
Posted by: CS1401 on: 9/19/2011 Level:Starter | Status: [Member] | Points: 10
@Yogendra_svim: friend i also using the master page.. can i know fully where u call the function and what error you get..
Posted by: Mittaikariss on: 9/22/2011 Level:Bronze | Status: [Member] | Points: 10
for window applications..
click the below link...i think this is also useful for web application
http://www.dotnetfunda.com/codes/code1755-how-to-clear-all-textbox-field-in-one-form.aspx

Login to post response