0

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

        }
1
  • 2
    Page always executes Page_Load handler - it's a part of the page lifecycle. It executes control events after that. Can you put breakpoint in the Votes_Click handler and see if it gets there after page load? Commented May 12, 2013 at 14:07

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

1 Comment

thank you for your answer but I have created dynamic controls in page_load event. if it runs again, it will produce an error of Multiple controls with the same ID. what I have to do??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.