ASP.Net:为什么我的按钮的单击/命令事件没有在转发器中绑定/触发?
这是来自具有中继器的 ascx 的代码:
<asp:Repeater ID="ListOfEmails" runat="server" >
<HeaderTemplate><h3>A sub-header:</h3></HeaderTemplate>
<ItemTemplate>
[Some other stuff is here]
<asp:Button ID="removeEmail" runat="server" Text="X" ToolTip="remove" />
</ItemTemplate>
</asp:Repeater>
在中继器数据绑定和事件的代码隐藏中:
Protected Sub ListOfEmails_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles ListOfEmails.ItemDataBound
If (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem) Then
Dim removeEmail As Button = CType(e.Item.FindControl("removeEmail"), Button)
removeEmail.CommandArgument = e.Item.ItemIndex.ToString()
AddHandler removeEmail.Click, AddressOf removeEmail_Click
AddHandler removeEmail.Command, AddressOf removeEmail_Command
End If
End Sub
Sub removeEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Write("<h1>click</h1>")
End Sub
Sub removeEmail_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
Response.Write("<h1>command</h1>")
End Sub
单击或命令都没有被调用,我做错了什么?
Here's the code from the ascx that has the repeater:
<asp:Repeater ID="ListOfEmails" runat="server" >
<HeaderTemplate><h3>A sub-header:</h3></HeaderTemplate>
<ItemTemplate>
[Some other stuff is here]
<asp:Button ID="removeEmail" runat="server" Text="X" ToolTip="remove" />
</ItemTemplate>
</asp:Repeater>
And in the codebehind for the repeater's databound and events:
Protected Sub ListOfEmails_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles ListOfEmails.ItemDataBound
If (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem) Then
Dim removeEmail As Button = CType(e.Item.FindControl("removeEmail"), Button)
removeEmail.CommandArgument = e.Item.ItemIndex.ToString()
AddHandler removeEmail.Click, AddressOf removeEmail_Click
AddHandler removeEmail.Command, AddressOf removeEmail_Command
End If
End Sub
Sub removeEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Write("<h1>click</h1>")
End Sub
Sub removeEmail_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
Response.Write("<h1>command</h1>")
End Sub
Neither the click or command is getting called, what am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这里有一个实验供您尝试:
在 ListOfEmails_ItemDataBound 上设置断点,然后查看是否调用它进行回发。
Here's an experiment for you to try:
Set a breakpoint on ListOfEmails_ItemDataBound and see if it's being called for postbacks.
你知道这有什么令人沮丧的吗?
如果您在该 asp:Button 标记中指定 OnClick,构建将验证代码隐藏类中是否存在命名方法,如果不存在,则报告错误...即使该方法将永远不会被打电话。
You know what's frustrating about this?
If you specify an OnClick in that asp:Button tag, the build will verify that the named method exists in the codebehind class, and report an error if it doesn't... even though that method will never get called.
如果您计划使用 ItemCommand 事件,请确保在 Page_Init 中而不是在 Page_Load 中注册 ItemCommand 事件。
我不确定为什么在 Page_Load 中注册的此事件对我不起作用,但将其移动到 Page_Init 有帮助。
If you're planning to use ItemCommand event, make sure you register to ItemCommand event in Page_Init not in Page_Load.
I am not sure why it wasn't working for me with this event registered in Page_Load but moving it to Page_Init helped.
您需要处理 ItemCommand 事件< /a> 在你的中继器上。 这是示例。
然后,您的按钮点击将由 ListOfEmails_ItemCommand 方法处理。 我认为在 ItemDataBound 中连接 Click 或 Command 事件(按钮的)不起作用。
You need to handle the ItemCommand event on your Repeater. Here's an example.
Then, your button clicks will be handled by the ListOfEmails_ItemCommand method. I don't think wiring up the Click or Command event (of the button) in ItemDataBound will work.
嵌套在中继器内的控件不会拦截事件。 相反,您需要绑定到Repeater.ItemCommand 事件。
ItemCommand
包含RepeaterCommandEventArgs< /code>
其中有两个重要字段:
那么,一个简单的例子:
Controls nested inside of Repeaters do not intercept events. Instead you need to bind to the
Repeater.ItemCommand
Event.ItemCommand
containsRepeaterCommandEventArgs
which has two important fields:So, a trivial example: