C# OnCheckedChanged 事件根本没有在 GridView 中触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1289149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OnCheckedChanged event not firing in GridView at all
提问by Adam
I have a GridView with an asp CheckBox in a TemplateField. The TemplateField is defined as follows:
我在 TemplateField 中有一个带有 asp CheckBox 的 GridView。TemplateField 定义如下:
<asp:TemplateField HeaderText="HeaderName">
<ItemTemplate>
<asp:CheckBox ID="checkBoxId" runat="server" OnCheckedChanged="MyCheckChangedMethod" AutoPostBack="true"/>
</ItemTemplate>
</asp:TemplateField>
When I run my web project with a breakpoint inside MyCheckChangedMethod and click the checkbox nothing happens. The breakpoint is not hit. My Visual Studio debugger is running.
当我在 MyCheckChangedMethod 中使用断点运行我的 Web 项目并单击复选框时,没有任何反应。断点未命中。我的 Visual Studio 调试器正在运行。
Additionally, I have AutoEventWireup = True in my page defnition so I don't have to manually hook up the event. I have never had a problem doing it this way before. I have a button on the same page setup the exact same way with a click event and the breakpoint gets hit fine in that.
此外,我的页面定义中有 AutoEventWireup = True,因此我不必手动连接事件。我以前这样做从来没有遇到过问题。我在同一页面上有一个按钮,以与单击事件完全相同的方式设置,并且断点在其中被击中。
Any ideas?
有任何想法吗?
采纳答案by CAbbott
The postback event for the checkbox control won't fire correctly because it's within a GridView that mangles the ID of the control.
复选框控件的回发事件不会正确触发,因为它位于破坏控件 ID 的 GridView 中。
If you need the Checkbox to reflect data you can use the CheckBoxFieldobject and bind that way.
如果您需要 Checkbox 来反映数据,您可以使用CheckBoxField对象并以这种方式绑定。
If you need it perform an action for the row, you may want to look at the ButtonFieldobject using the CommandNameproperty and the RowCommandevent.
如果您需要它为行执行操作,您可能需要使用CommandName属性和RowCommand事件查看ButtonField对象。
There are ways to access the checkboxes within the GridView server side.
有多种方法可以访问 GridView 服务器端中的复选框。
回答by Ganesh R.
try:
尝试:
<asp:CheckBox ID="checkBoxId" runat="server" AutoPostBack=true OnCheckedChanged="MyCheckChangedMethod"/>
Make sure that the aspx page has CodeFile="YOUR_FILE.aspx.cs" at the top.
确保 aspx 页面顶部有 CodeFile="YOUR_FILE.aspx.cs"。
Also see to it that your function MyCheckChangedMethod is defined as
还要注意你的函数 MyCheckChangedMethod 被定义为
Function should have object sender, EventArgs e.
函数应该有对象发送者,EventArgs e。
public void MyCheckChangedMethod(object sender, EventArgs e)
{
bool b = false;//your data here
}
Also make sure that the web.config has debug set to true (think already done).
还要确保 web.config 已将调试设置为 true(认为已经完成)。
回答by royalnight102
You need to add AutoPostback = True
in asp:CheckBox
tag.
您需要添加AutoPostback = True
的asp:CheckBox
标签。
回答by Eduard Grinberg
The problem occurs when DataBind
is called before the control event is firing.
If you call DataBind
in Page_Load
put it in
if (!isPostBack) {}
and call DataBind
in the event handler itself.
在DataBind
触发控件事件之前调用时会出现问题。如果你调用DataBind
在Page_Load
放它在
if (!isPostBack) {}
和呼叫DataBind
事件处理程序本身。