Why in ASP.NET is a button click event executes when page is refreshed?
In my ASP.Net Web Site I have a button.When I click the button and then reload the page via browser,the click event of the button fires.Where is a probl开发者_开发技巧em,please help me.
If I understand correctly.
You have a web form with a button.
You push the button which causes a post back and the event handler for the button press to execute.
Then you hit refresh and the page has the button event handler execute again.
The reason for this is your refreshing the last information sent to the server. Which is the button click information in the __doPostback. This is why you are seeing the event of the button fire again.
Here is an article talking about how to detect a refresh over a postback.
It's because clicking that button sends a POST request to your page. The POST data is kept in the http headers and when you refresh, it's sent again to server.
Your browser should warn you when you try to refresh the page.
This is by design. When you click a server side button (with the runat="server"
attribute), a click will cause a postback and the button click event will fire.
If you want some client side behaviour, you need to use the OnClientClick
attribute, as described in this MSDN article (How to: Respond to Button Web Server Control Events in Client Script).
If this is really important for someone, then they can refresh the page again through a Response.Redirect(). This is the easiest solution that I have been able to find.
the easiest way to solve this issue is to redirect your page to some url or refresh your current page using Response.Redirect(Request.RawUrl);
I had the same issue and it was solved by putting the button as asp:AsyncPostBackTrigger of the updatePanel.
If you want to refresh Part of your page then put the control inside the UpdatePanel
if the control causes PostBack
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button CssClass="btn btn-default"
onClick="uploadAttachmentToList" runat="server"
ID="btnUpload" ClientIDMode="Static" Text="Upload"
/>
</ContentTemplate>
</asp:UpdatePanel>
精彩评论