In this article I will explain with an example, how to clear (reset) Form fields (data) after form submit (PostBack) in ASP.Net using C# and VB.Net.
Though Form fields can be cleared (reset) by setting each field to its default value but this process could be tedious when there are too many fields in the form.
 
 
HTML Markup
The following HTML Markup consists of a standard form with some fields and a Button to submit the form and insert the data into database.
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td>
            Name:
        </td>
        <td>
            <asp:TextBox ID="txtName" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            City:
        </td>
        <td>
            <asp:TextBox ID="txtCity" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Country:
        </td>
        <td>
            <asp:TextBox ID="txtCountry" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
            <asp:Button ID="btnSave" Text="Save" runat="server" OnClick="Save" />
        </td>
    </tr>
</table>
 
 
Clear (Reset) Form fields (data) after form submit (PostBack) in ASP.Net
When the Submit Button is clicked, first the record is inserted into the database and then using ClientScript RegisterStartupScript function, the success message is displayed in JavaScript alert message box.
message – The message to be displayed in the JavaScript Alert Message Box.
script – The JavaScript code that will display the Alert message box.
Once the OK button of the JavaScript Alert Message Box is clicked, the form fields (data) is cleared (reset) by redirecting to the same page using JavaScript window.location function.
Note: Considering the scope of the article, the code for insertion of record to database is not covered in this article.
 
C#
protected void Save(object sender, EventArgs e)
{   
    //Insert record here.
 
    //Display success message and clear the form.
    string message = "Your details have been saved successfully.";
    string script = "window.onload = function(){ alert('";
    script += message;
    script += "');";
    script += "window.location = '";
    script += Request.Url.AbsoluteUri;
    script += "'; }";
    ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true);
}
 
VB.Net
Protected Sub Save(sender As Object, e As EventArgs)
    'Insert record here.
 
    'Display success message and clear the form.
    Dim message As String = "Your details have been saved successfully."
    Dim script As String = "window.onload = function(){ alert('"
    script &= message
    script &= "');"
    script &= "window.location = '"
    script &= Request.Url.AbsoluteUri
    script &= "'; }"
    ClientScript.RegisterStartupScript(Me.GetType(), "SuccessMessage", script, True)
End Sub
 
Clear (Reset) Form fields (data) after form submit (PostBack) in ASP.Net
 
And if you want to clear the form without any success message then refer the following method where the form fields (data) is cleared (reset) by redirecting to the same page using Response.Redirect method.
C#
protected void Save(object sender, EventArgs e)
{
    //Insert record here.
 
    //Redirect to clear the form.
    Response.Redirect(Request.Url.AbsoluteUri);
}
 
VB.Net
Protected Sub Save(sender As Object, e As EventArgs)
    'Insert record here.
 
    'Redirect to clear the form.
    Response.Redirect(Request.Url.AbsoluteUri)
End Sub
 
Clear (Reset) Form fields (data) after form submit (PostBack) in ASP.Net
 
 
Demo
 
 
Downloads