Reputation: 1
I need to reset my values in text box with out reloading the page. Below is the which i use currently for reset but on click of reset page is reloaded. Can any one please advise the code to reset without page reload.Is it possible only through Javascript
protected void BtnReset_Click(object sender, EventArgs e)
{
txt1.Text = string.Empty;
txt2.Text = string.Empty;
}
Upvotes: 0
Views: 2742
Reputation: 1489
One option is to wrap all of your controls in an UpdatePanel. You'll still have a postback, but it will be done via AJAX and won't refresh the [entire] page (so you wont get the flicker). That will let you use the function you wrote.
If you don't need the postback and there is nothing that you need/want to handle serverside, then use the HTML reset button as Pradeep suggested.
Upvotes: 1
Reputation: 116
you can use html
<input type="reset" value="reset"/>
this will clear the form fields value
Upvotes: 1
Reputation: 62300
Here is the jQuery that will let you rest the specific textboxes.
You can also use regular input button since it doesn't require post back.
<asp:TextBox runat="server" ID="txt1" />
<asp:TextBox runat="server" ID="txt2" />
<asp:Button ID="btn1"
runat="server"
OnClientClick="return resetButtonClick();"
Text="Reset" />
<input type="submit" value="Reset"
onclick="return resetButtonClick();" />
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
function resetButtonClick() {
$("#<%= txt1.ClientID %>").val("");
$("#<%= txt2.ClientID %>").val("");
return false;
}
</script>
Upvotes: 0
Reputation: 945
If you want to reset your whole form, just replace your asp.net button, with an HTML one.
So replace something like this:
<asp:Button ID="BtnReset" runat="server" Text="Button" />
with:
<input id="Reset1" type="reset" value="reset" />
(type="reset" : will reset your form to original values)
Upvotes: 0
Reputation: 916
I believe in Javascript it's just:
document.getElementById("txt1").value = "";
document.getElementById("txt2").value = "";
Upvotes: 0