找出与我所拥有的相匹配的ID -作为保持内容

本文关键字:ID 拥有 | 更新日期: 2023-09-27 18:11:08

如何将复选框获取到中继器中,并且进入复选框必须有一个Id,并且它是唯一的。

例如,当我点击2和5和更新按钮时,我如何抓取复选框,我不能用普通的复选框做,但可以用普通的复选框。

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.
    }
更新:

<asp:CheckBoxList ID="CheckBoxListList" runat="server">
                            <asp:ListItem Value="<%# Eval("id") %>"></asp:ListItem>
                        </asp:CheckBoxList>

找出与我所拥有的相匹配的ID -作为保持内容

参见MSDN上的示例或相关问题:如何在ASP中使用foreach获取CheckBoxList中选定项的值。净c#吗?

可能的示例,其中id存储在selectedIds列表

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);
        }
    }
}

您可以使用以下技巧

  1. 在下面的输入复选框中使用RunAt="Server",这样从服务器生成的id将是唯一的

 <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点击代码如下

 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...
        }
    }
}