Clear all Label and GridView controls data using C# and VB.Net in ASP.Net

Last Reply one month ago By dharmendr

Posted one month ago

I want to try and make it possible to clear both labels and gridview control values from a particular <div> tag when I navigate to another page. How can this be done please?

Posted one month ago Modified on one month ago

Hi George616,

To clear a Label set the Text property to an empty string.

Same way to clear GridView set the DataSource property yo null.

Create a method like below and call when ever required.

C#

private void ClearControl()
{
    GridView1.DataSource = null;
    GridView1.DataBind();
    Label1.Text = "";
    Label2.Text = "";
}

VB.Net

Private Sub ClearControl()
    GridView1.DataSource = Nothing
    GridView1.DataBind()
    Label1.Text = ""
    Label2.Text = ""
End Sub