0

I use a Checkbox in a Repeater, how can I know which Checkbox have changed in OnCheckedChanged? I have tried to set id then checkbox is binding data, but it will not work. Hope someone can help me

Thanks /Mats

4

2 回答 2

2

检查sender(Event Target)参数

protected void Chb_Changed(object sender, EventArgs e)
{
    if (sender != null)
    {
      CheckBox cb=(CheckBox)sender;

      string clickedCheckBoxID=cb.ID;
    }
}
于 2013-10-14T13:44:12.703 回答
0

尝试跟随。请注意,我们还可以绑定一些主列,例如在某个隐藏字段中的“ID”列,然后进入代码后面。

ASPX 侧

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="sqldtasource" >
       <ItemTemplate>
       <asp:CheckBox ID="chk" runat="server" AutoPostBack="true" Text='<%#Bind("Name")%>' OnCheckedChanged="Chb_Changed"/>
        <asp:HiddenField ID="hdn_ID" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "ID")  %>'/>
       </ItemTemplate>
 </asp:Repeater>

代码背后:

protected void Chb_Changed(object sender, EventArgs e)
    {
        if (sender != null)
        {
            try
            {
                 var hdnID = (HiddenField)checkBox.NamingContainer       .FindControl("hf_ID");
                if(hdnID != null)
                 {
                   string primaryFieldValue = hdnID.Value;
                 }

                if (((CheckBox)sender).Checked)
                {
                    Response.Write(((CheckBox)sender).Text + " is checked");
                }
            }
            catch { 

                  }
        }
    }
于 2013-10-14T13:24:42.473 回答