Powered by Blogger.

Wednesday, May 7, 2014

Clear textboxes inside a form in C#.Net

In this post we will discuss how to clear textboxes inside a form in C#.Net.

Also you can check out:

- Remove column from datatable in Asp.Net

- How to handle exception in sql server stored procedure?

- Array and ArrayList in C#.Net

If you have very less controls in a form then you can clear like below:

txtName.Text = "";
txtAge.Text= "";``````````

But if you have more controls in the form then you can clear the values like below:

Below is the full method:

public void CleartextBoxes(Control parent)

        foreach (Control x in parent.Controls)
            if ((x.GetType() == typeof(TextBox)))
                ((TextBox)(x)).Text = "";

            if (x.HasControls())

You can call the method from button click like below:

protected void Button1_Click(object sender, EventArgs e)

This code will clear all the textboxes presented in the form.


Post a Comment