How to clear more than one textboxes values when button is clicked. [Resolved]

Posted by Srinibaschampati under C# on 8/14/2013 | Points: 10 | Views : 1628 | Status : [Member] | Replies : 4
How to clear more than one textboxes values when button is clicked.




Responses

Posted by: Satyapriyanayak on: 8/14/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
foreach (Control Cleartext in this.Controls)
{

if (Cleartext is TextBox)
{

((TextBox)Cleartext).Text = string.Empty;

}

}

}
}
}


If this post helps you mark it as answer
Thanks

Srinibaschampati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 8/14/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Another way

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.Controls.Count; i++)
{

if (this.Controls[i] is TextBox)
{

this.Controls[i].Text = "";

}
}


}
}
}


If this post helps you mark it as answer
Thanks

Srinibaschampati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 8/14/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
protected void Button2_Click(object sender, EventArgs e)

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


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Srinibaschampati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akshayyy on: 8/14/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,
U can easily do so by writing a Clear method and calling this method on the button click.
for eg:
private void Clear()
{
textbox1.text = string.Empty;
textbox2.text = string.Empty;
textbox3.text = string.Empty;
...
....
}

Like wise u can clear the text of as many text boxes that are present on the page.
Hope this Helps.

Srinibaschampati, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response