Feras Salim
Feras Salim

Reputation: 438

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

        }

Upvotes: 0

Views: 1438

Answers (2)

Michael Bowersox
Michael Bowersox

Reputation: 1481

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.

Upvotes: 3

Brian
Brian

Reputation: 243

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

Upvotes: 3

Related Questions