Why in ASP.NET is a button click event executes wh

2020-02-05 02:03发布

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 problem,please help me.

标签: asp.net
7条回答
萌系小妹纸
2楼-- · 2020-02-05 02:25

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).

查看更多
迷人小祖宗
3楼-- · 2020-02-05 02:29

I had the same issue and it was solved by putting the button as asp:AsyncPostBackTrigger of the updatePanel.

查看更多
成全新的幸福
4楼-- · 2020-02-05 02:34

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.

查看更多
你好瞎i
5楼-- · 2020-02-05 02:34

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>
查看更多
萌系小妹纸
6楼-- · 2020-02-05 02:39

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);

查看更多
We Are One
7楼-- · 2020-02-05 02:40

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.

查看更多
登录 后发表回答