0

How do you get hold of a checkbox into a Repeater, and into the checkbox must have an Id, and it is unique.

When I for example have click 2 and 5 and Update antalt by button, how have I grab checkbox, I can not do it with ordinary checkbox but can with ordinary checkbox.

shop.aspx

<asp:Repeater ID="RepeaterList" runat="server">
                <ItemTemplate>
                    <tr>
                        <td>
                            <input  id="CheckboxValue" type="checkbox" style="width: 20px;" value="<%# Eval("id") %>" />
                        </td>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>

<asp:Button ID="ButtonUpdate" OnClick="ButtonUpdate_Click" runat="server" CssClass="butikkenClick" Text="Opdatere kurv" />

shop.aspx.cs file her:

protected void ButtonUpdate_Click(object sender, EventArgs e)
    {
    //This is where I need to find out which checkbox is click on and after I will update the content of the ID and so I have a textbox next.
    }

Update:

<asp:CheckBoxList ID="CheckBoxListList" runat="server">
                            <asp:ListItem Value="<%# Eval("id") %>"></asp:ListItem>
                        </asp:CheckBoxList>
Nick Willumsen
  • 100
  • 2
  • 10

2 Answers2

0

See Examples on MSDN or this related question: How to get values of selected items in CheckBoxList with foreach in ASP.NET C#?

Possible example, where the Ids are being stored in a selectedIds list

protected void ButtonUpdate_Click(object sender, EventArgs e)
{
    var selectedIds = new List<string>();
    foreach (ListItem item in CheckBoxListList.Items)
    {
        if (item.Selected) 
        {
           selectedIds.Add(item.Value);
        }
    }
}
Community
  • 1
  • 1
stephen.vakil
  • 3,492
  • 1
  • 18
  • 23
0

You may use the below technique

  1. use RunAt="Server" in the below input CheckBox so that id generated from server and it will be unique

 <asp:Repeater ID="RepeaterList" runat="server">
            <ItemTemplate>

                <input id="CheckboxValue" runat="server" type="checkbox" style="width: 20px;" value='<%# Eval("id") %>' />

            </ItemTemplate>
        </asp:Repeater> 

update Button Click code will be as follows

 protected void ButtonUpdate_Click(object sender, EventArgs e)
{
    foreach (RepeaterItem thisItem in RepeaterList.Items)
    {
        var chkBox = ((CheckBox)thisItem.FindControl("CheckboxValue"));
        if (chkBox.Checked)
        {
            string PKValue = chkBox.Attributes["value"];
            //Use this for DB update...
        }
    }
}