我刚刚将button
从html
移到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
永远不会被解雇。
请帮助我。
答案 0 :(得分:2)
ContentPlaceHolder
与NamingContainer
不同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