简体   繁体   English

取消选中时,ASP.NET C#CheckBox不会触发CheckedChanged事件

[英]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.

相关问题 在 ASP.Net 中动态取消选中复选框时,CheckBox 不会触发 CheckedChanged 事件 - CheckBox does not fire CheckedChanged event when unchecking the checkbox Dynamically in ASP.Net 通过在asp.net中取消选中它,Checkbox消失了 - Checkbox disappeared by unchecking it in asp.net 如何在ASP.NET的ListView中的CheckedChanged事件的复选框中找到数据键? - How to find the data key on CheckedChanged event of checkbox in ListView in ASP.NET? 为什么在取消选中asp.net中的复选框时不会触发checkchanged事件 - why does checkchanged event does not fire on uncheck of checkbox in asp.net 动态地将事件CheckedChanged添加到后台页面C#中的checkBox - Dynamically add event CheckedChanged to checkBox in back page C# 当取消选中复选框时,asp:复选框的OnCheckedChanged事件处理程序不会触发 - OnCheckedChanged event handler of asp:checkbox does not fire when checkbox is unchecked UpdatePanel中的C#ASP.NET CheckBox在“更新期间”单击时未注册单击 - C# ASP.NET CheckBox in UpdatePanel does not register click when clicked “during” update C# asp.net 复选框未选中时不触发 - C# asp.net checkbox not firing when unchecked Winform Checkbox MouseDown事件处理程序不允许触发CheckedChanged事件 - Winform Checkbox MouseDown event handler not allowing CheckedChanged event to fire ASP.NET C#4复选框验证 - asp.net c# 4 checkbox validation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM