[英]ASP.NET C# CheckBox does not fire CheckedChanged event when unchecking
I have repeater 我有中继器
<asp:Repeater ID="rptResult" runat="server" >
<ItemTemplate>
<tr>
<td>
<asp:CheckBox ID="chkShipStatus" runat="server" EnableViewState="true" ViewStateMode = "Enabled" AutoPostBack="True" CommandName='<%# DataBinder.Eval(Container.DataItem, "CT")%>' OnCheckedChanged="chkShipStatus_CheckedChanged" Checked='<%# Convert.ToBoolean(Eval("SHIPPED")) ? true : false %>' />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
and 和
and call data from page_load 并从page_load调用数据
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = ap.GetDataTable(sql);
rptResult.DataSource = dt;
rptResult.DataBind();
}
//go there and finish when unchecd
}
and here is my CheckedChanged event it call only when i cheched not firing when i unchecked it go to page_load(not incycle !postback) 这是我的CheckedChanged事件,仅当我检查不触发时才调用它,当我未选中它时转到page_load(不循环!回发)
protected void chkShipStatus_CheckedChanged(object sender, EventArgs e)
{
var cb = (CheckBox)sender;
//go there when i checked
if (cb.Checked == true)
{
//go there when i checked
}
else
{
//never go in here when i uncheck
}
}
A CheckBox
has no CommandName
property so remove it CheckBox
没有CommandName
属性,因此请将其删除
<asp:CheckBox ID="chkShipStatus" runat="server" EnableViewState="true" ViewStateMode = "Enabled"
AutoPostBack="True"
OnCheckedChanged="chkShipStatus_CheckedChanged"
Checked='<%# Convert.ToBoolean(Eval("SHIPPED")) ? true : false %>'
/>
if you want to know in which item the checkbox was you can use NamingContainer
. 如果您想知道复选框位于哪个项目中,则可以使用
NamingContainer
。 Then use repeaterItem.FindControl
to get another control in that RepeaterItem
. 然后使用
repeaterItem.FindControl
在该RepeaterItem
获取另一个控件。 So you could for example use a HiddenField
to store the ID of the record. 因此,例如,您可以使用
HiddenField
来存储记录的ID。
protected void chkShipStatus_CheckedChanged(object sender, EventArgs e)
{
var cb = (CheckBox)sender;
RepeaterItem item = (RepeaterItem) cb.NamingContainer
HiddenField hiddenID = (HiddenField) item.FindControl("HiddenID");
string id = hiddenID.Value;
// ...
}
you say : 你说 :
it call only when i cheched not firing when i unchecked it go to page_load(not incycle !postback)
它仅在我取消选中时不触发时才调用它去page_load(not incycle!postback)
so what is the code in the load ?? 那么负载中的代码是什么? it may has some code that prevents the uncheck process
它可能有一些阻止取消检查过程的代码
If you have SHIPPED column values like 1 OR 0 then I recommend you to update below attribute of your checkbox control. 如果您具有SHIPPED列值(例如1或0),那么我建议您更新复选框控件的以下属性。 I tried it and chkShipStatus_CheckedChanged event calling for me.
我尝试了它,并调用了chkShipStatus_CheckedChanged事件。
Checked='<%# Convert.ToString(Eval("SHIPPED")) == "1" ? true : false %>'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.