如何将复选框保存到Repeater中,并且复选框必须具有Id,并且它是唯一的。
例如,当我点击2和5以及按钮更新antalt时,我如何抓住复选框,我无法使用普通复选框,但可以使用普通复选框。
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提交她:
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>
答案 0 :(得分:0)
请参阅Examples on MSDN或此相关问题:How to get values of selected items in CheckBoxList with foreach in ASP.NET C#?
可能的例子,其中Ids存储在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 :(得分:0)
您可以使用以下技巧
<asp:Repeater ID="RepeaterList" runat="server">
<ItemTemplate>
<input id="CheckboxValue" runat="server" type="checkbox" style="width: 20px;" value='<%# Eval("id") %>' />
</ItemTemplate>
</asp:Repeater>
&#13;
更新按钮点击代码如下
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...
}
}
}