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())
            {
                CleartextBoxes(x);
            }
        }
    }

You can call the method from button click like below:

protected void Button1_Click(object sender, EventArgs e)
    {
        CleartextBoxes(this);
    }

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





0 comments

Post a Comment