Repeater绑定checkbox循环输出后,怎样将选定的值保存到数据库
前台代码:
<asp:Repeater ID="rep_jy" runat="server">
<ItemTemplate>
<asp:CheckBox ID='cb' Text='<%#Eval("t_name") %>' runat="server" />
</ItemTemplate>
</asp:Repeater>
[code=C#][/code]
string message = string.Empty;
foreach (RepeaterItem item in this.rep_jy.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
CheckBox cb = item.FindControl("cb") as CheckBox;
if (cb.Checked)
{
message += cb.Text + ",";
}
}
}
------解决方案--------------------string message = string.Empty;
打开数据库
foreach (RepeaterItem item in this.rep_jy.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
CheckBox cb = item.FindControl("cb") as CheckBox;
if (cb.Checked)
{
message = "INSERT INTO table values(@message)";
cmd = new SqlCommand(message,cn);
cmd.Parameters.AddWithValue("@message",cb.Text);
cmd.ExecuteNonQuery();
}
}
}
------解决方案--------------------直接根据你前台的值拼结好sql语句,用sqlcommand执行下就好了,这可是最基本的数据操作噢
------解决方案--------------------ID是不能Eval绑定的
------解决方案-------------------- <asp:CheckBox ID='cb' Text='<%#Eval("t_name") %>' runat="server" />
CheckBox cb = item.FindControl("cb") as CheckBox;就可以啊,你为啥又改成
<asp:CheckBox ID='cb_<%#Eval("ID")%>' ??
这就完全错了