ASP.Net将事件处理程序分配给asp按钮单击

时间:2013-05-12 14:03:54

标签: c# asp.net

我正在尝试将事件处理程序分配给asp按钮单击

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

但是在调试项目并单击上面的按钮时,它会执行page_load事件而不是Votes_Click事件

为什么这样做? 以及如何解决这个问题???

并且页面后面代码的句柄是

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

        }

2 个答案:

答案 0 :(得分:3)

当您的网页发回时,它将始终执行Page_Load()。然后它应继续到您的事件处理程序。如果你继续调试,它会转到你的点击事件处理程序吗?

请参阅ASP.NET Page Life Cycle Overview

答案 1 :(得分:3)

这是设计的。如果要避免在回发事件中执行Page_Load事件中的代码,可以在条件语句中使用Page.IsPostBack属性。