为什么 GridView 内的 LinkButton 不会引发其 OnClick 事件?
我在 GridView 中有一个 LinkButton(通过 TemplateField)。无论我如何尝试,LinkButton 都不会调用其事件处理程序。我尝试过以下两种方法:
- 传统的事件处理程序(“OnClick”)
- GridView 级别的 OnRowCommand 事件处理程序。
在这两种情况下,我都进行了调试,但它甚至没有捕获事件处理程序。
如果我将 LinkButton 移出页面(因此它不在 GridView 中),它会正常工作,因此我知道语法是正确的。
这是“传统”方法:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Text="Cancel" ID="DeleteButton" CausesValidation="false" OnClick="CancelThis" runat="server" />
</ItemTemplate>
<asp:TemplateField>
有趣的是,如果我从后面的代码中删除“CancelThis”方法,它会抛出一个错误。所以我知道它知道它的事件处理程序,因为它在编译时会查找它。
下面是 RowCommand 方法:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Text="Cancel" ID="DeleteButton" CausesValidation="false" CommandName="CancelThis" runat="server" />
</ItemTemplate>
<asp:TemplateField>
在本例中,GridView 具有:
OnRowCommand="GridView_RowCommand"
它回发,但从不提示引发事件。
知道我在这里缺少什么吗?
I have a LinkButton inside a GridView (via an TemplateField). No matter what I try, the LinkButton will not invoke its event handler. I have tried both:
- A traditional event handler ("OnClick")
- A OnRowCommand event handler at the GridView level.
In both cases, I've debugged and it doesn't even catch the event handler.
If I move the LinkButton out on the page (so it's not in the GridView), it works fine, so I know the syntax is right.
Here is the "traditional" method:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Text="Cancel" ID="DeleteButton" CausesValidation="false" OnClick="CancelThis" runat="server" />
</ItemTemplate>
<asp:TemplateField>
What's interesting is if I remove the "CancelThis" method from the code behind, it throws an error. So I know it's aware of its event handler, because it looks for it when it compiles.
Here is the RowCommand method:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Text="Cancel" ID="DeleteButton" CausesValidation="false" CommandName="CancelThis" runat="server" />
</ItemTemplate>
<asp:TemplateField>
In this case, the GridView has:
OnRowCommand="GridView_RowCommand"
It postsback, but never hints at raising the event.
Any idea what I'm missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你如何绑定你的 GridView ?您使用数据源控件吗?如果您在
Page_Load
期间手动绑定,则可能由于网格每次往返都进行绑定,因此事件处理程序无法正确捕获。如果是这种情况,您可能需要尝试以下操作:您可以发布示例绑定代码来配合您的标记吗?
如果您确实想要强制解决此问题,您可以挂钩网格上的 RowDataBound 事件,手动找到按钮并在后面的代码中添加处理程序。类似于:
标记片段:
代码隐藏:
我不确定该按钮的用途是什么,但假设它是行删除按钮,您可能不想采用这种方法,因为在事件处理程序中,您没有直接访问相关行,就像使用
RowCommand
事件一样。您使用模板字段有什么原因吗? VS 说一个
ButtonField
?如果您使用ButtonField
,则可以挂钩RowCommand
事件。标记片段:
代码隐藏:
您可能需要查阅有关其中一些主题的 MSDN 文档:
编辑:
回答您关于 ButtonField 的问题 - 是的,我不明白为什么您仍然不能处理按钮字段。这是一个在行数据绑定期间查找按钮字段并隐藏它的片段(未经测试,但我认为会起作用......)
How are you binding your
GridView
? Are you using a datasource control? If you are binding manually duringPage_Load
, it's possible that since the grid is binding every round trip, the event handler isn't catching properly. If this is the case, you may want to try something like:Can you post sample binding code to go with your markup?
If you really want to force the issue, you could hook into the RowDataBound event on the Grid, find the button manually and add the handler in the code behind. Something like:
markup snippet:
code behind:
I'm not sure what the purpose of the button is, but assuming it is a row delete button, you may not want to take this approach as in the event handler, you don't have direct access to the row in question, like you would using the
RowCommand
event.Is there a reason you're using the Template field? Vs say a
ButtonField
? If you use aButtonField
, then you can hook into theRowCommand
event.markup snippet:
code behind:
You might want to consult MSDN docs on some of these topics:
EDIT:
To answer your question on the ButtonField - yes I don't see why you couldn't still deal with a buttonfield. Here's a snippet to find the buttonfield during row data bound and hide it (untested but I think would work...)
你的 GridView 上的视图状态打开了吗?这让我多次陷入困境。
Is viewstate turned on on your GridView? This has caught me out numerous times.