移动到asp:content后,按钮单击事件未触发

时间:2015-04-24 08:42:17

标签: asp.net vba

我刚刚将buttonhtml移到asp:content,因为我使用了master page

<asp:content id="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div>
     <asp:CustomValidator ID="CustomValidator1" runat="server" 
            ErrorMessage="CustomValidator"></asp:CustomValidator>
            <br />
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <br />
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Upload" />
    </div>
</asp:content>

以下代码在我移动它之前工作,现在点击事件没有被触发:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

它给了我一个错误(此错误之前没有发生):

  

Handles子句需要在包含类型或其基类型之一中定义WithEvents变量。

所以我添加了以下代码行:

Private WithEvents Button1 As Button
  

但是,button1永远不会被解雇。

请帮助我。

1 个答案:

答案 0 :(得分:2)

ContentPlaceHolderNamingContainer不同Page(它实现INamingContainer),因此如果声明变量{{},则不会自动从ASP.NET初始化Private WithEvents Button1 As Button 1}}。

您必须以声明方式(或从代码隐藏中以编程方式)附加事件处理程序:

<asp:Button ID="Button1" OnClick="Button1_Click" runat="server" Text="Upload" />

并删除Handles子句,方法必须现在至少为Protected

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) 

End Sub