简体   繁体   中英

ASP.Net assign event handler to asp button click

I'm trying to assign event handler to the asp button click

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <div>
       <asp:Table id="tb" runat="server" Height="239px" Width="417px" >

       </asp:Table>

           </div>
    <table>
          <tr>
              <td></td><td><asp:Button ID="Votes" runat="server" Text="Vote" OnClick="Votes_Click" /></td>
          </tr>
      </table>

</asp:Content>

But when debugging the project and click the above button it execute page_load event not Votes_Click event

why it does that??? and how to solve this problem???

and the handle on code behind page is

protected void Votes_Click(object sender, EventArgs e)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "btn",

     "<script type = 'text/javascript'>alert('Button Clicked');</script>");
            int i = 0;
            Response.Redirect("Default.aspx");

        }

When your page posts back, it will always do a Page_Load(). It should then continue on to your event handlers. If you continue debugging, does it get to your click event handler?

See ASP.NET Page Life Cycle Overview

This is by design. If you want to avoid executing the code in the Page_Load event in the event of a postback, you can use the Page.IsPostBack property in a conditional statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM