简体   繁体   中英

CheckBox not Working from inside a Repeater in ASP.NET Webforms

This is what I wrote inside the Repeater

<td style="text-align: center; width: 60px">
   <asp:CheckBox ID="DeleteCheckBox" runat="server" CommandArgument='<%# Eval("Id")%>' AutoPostBack="True" OnCheckedChanged="DeleteCheckBox_Click" />
td>

and this is the C#

protected void DeleteLinkbtn_Click(object sender, EventArgs e)
    {
        try
        {
            LinkButton BtnId = (LinkButton)(sender);
            string ButtonId = BtnId.CommandArgument;
            long Active = 1;
            if (ButtonId !=string.Empty) 
            {
                if (DeleteCheckBox.Checked==true)
                {
                    objdalTransactionEntry.RentSettingsIsActiveUpdate(Convert.ToInt64(ButtonId), Active);
                }
                
            }
        }
        catch (Exception) { }
    }

The Problem is that it is not being able to find the ID of the checkbox DeleteCheckBox in C# code

Based on you code, I have the following comments:

  1. Change you function name, it's confusing. It should either be DeleteCheckBox_Changed or it should not be used for the OnCheckedChanged event

  2. Your sender is not a LinkButton it is the checkbox itself. So you can access the checkbox control with CheckBox checkBoxThatChanged = (CheckBox)(sender)

  3. DeleteCheckBox does not really exist. You have multiple DeleteCheckBox s due to your repeater, so you won't be able to access it this way.

  4. If you are indeed using a LinkButton , and you want to find a checkbox, then you should add a value to the link button, indicating the row of the table it refers to and iterate through all the checkbox controls until you find your target one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM