Clear all textbox in C#

Posted by Mohansubramanian under C# on 3/26/2013 | Points: 10 | Views : 49532 | Status : [Member] | Replies : 7
Hi,

I need common code for clear all textbox avaliable in one form by single method in C#.

Regards,
Mohan
powerofmicrosoft.blogspot.in

Regards,
Mohan Subramanian
http://mohanks.com



Responses

Posted by: Ankitsrist on: 3/26/2013 [Member] Starter | Points: 25

Up
0
Down
public void clear()
{
TextBox1.Text="";
TextBox2.Text="";
and so on

}
and call this function in page load or on any button click

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

Posted by: Jayakumars on: 3/26/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi

try this code

protected void Bt_Submit1_Click1(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);
}
}

refer this url

http://www.codeproject.com/Tips/255136/How-to-Clear-all-the-TextBox-value-in-Single-Click

Mark as Answer if its helpful to you

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

Posted by: Sandeepmhatre on: 3/26/2013 [Member] Starter | Points: 25

Up
0
Down
refer this link


set text box values to string.Empty
for explaination in details,

http://sandeepmhatre.blogspot.com/2013/03/difference-between-stringempty-string.html

Sandeep M,
Software Developer
Follow me on :
http://sandeepmhatre.blogspot.in

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

Posted by: Satyapriyanayak on: 3/26/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Method-1
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 Clear_textbox_windows
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
CleartextBoxes1();
}

public void CleartextBoxes1()
{
foreach (Control Cleartext in this.Controls)
{

if (Cleartext is TextBox)
{

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

}

}

}
}
}

Method-2
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 Clear_textbox_windows
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
CleartextBoxes2();
}
public void CleartextBoxes2()
{

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

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

Posted by: aswinialuri-19361 on: 3/26/2013 [Member] Starter | Points: 25

Up
0
Down
hi
try this and it is very useful to you
In clear click event
{

clear(Page.Controls);
}

private void clear(ControlCollection ctrl)
{
foreach (Control ctrl1 in ctrl)
{
if (ctrl1 is TextBox)
((TextBox)ctrl1).Text = string.Empty;
clear(ctrl1.Controls);
}
or else if you want to clear all drop downlist what ever it may be you will use
Thanks & Regards



Mark as Answer if it helps you
Thanks&Regards
Aswini Aluri

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

Posted by: Rimi1289 on: 4/26/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,

You can use JQuery to clear all values on the page or in a container. JQuery is simple and easy to use. It also simplifies the coding and keeps it short. Check the link and let me know if it helped.

http://www.encodedna.com/2013/04/clear-all-texbox-using-jquery.htm

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

Posted by: Satyapriyanayak on: 4/26/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Method-1
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 Clear_textbox_windows
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
CleartextBoxes1();
}

public void CleartextBoxes1()
{
foreach (Control Cleartext in this.Controls)
{

if (Cleartext is TextBox)
{

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

}

}

}
}
}

Method-2
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 Clear_textbox_windows
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
CleartextBoxes2();
}
public void CleartextBoxes2()
{

for (int i = 0; i < this.Controls.Count; i++)
{

if (this.Controls is TextBox)
{

this.Controls.Text = "";

}
}
}
}
}


If this post helps you mark it as answer
Thanks

If this post helps you mark it as answer
Thanks

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

Login to post response