checkbox动态生成绑定数据?
- C# code
string[] groupSub = groupList.Split(','); sql = string.Empty; for (int i = 0; i < groupSub.Length; i++) { sql = "select id,user_truename from l_user where id='" + Convert.ToInt32(groupSub[i].ToString()) + "'"; //有问题 table = DbHelperSQL.Query(sql).Tables[0]; dlSelectUser.DataSource = table; dlSelectUser.DataBind(); }我来解释下我的代码:
第一行的grouplist是一个字符串,该字符串用逗号分隔用户id,比如(1,3,5,7)等是用户id
然后我for循环每一个用户id,sql语句每得到一个id都会到数据库去查询每个id的用户信息
然后绑定到datalist里有一个checkbox,现在问题来了,我的这个checkbox只能得到最后一个用户的信息
前面的都被覆盖了,我想问的是如何得到所有用户信息并绑定checkbox?
[解决办法]
checkbox?也能用来绑定么?
昏
你可以用repeter来绑定
循环里面放checkbox
你查多少条数据有多少个checkbox
[解决办法]
- C# code
string[] groupSub = groupList.Split(','); System.Text.StringBuilder sb = new System.Text.StringBuilder(); for (int i = 0; i < groupSub.Length; i++) { int n = 0; if (int.TryParse(groupSub[i], out n)) { sb.Append(groupSub[i] + ","); } } if (sb.Length > 0) { sb.Remove(sb.Length - 1, 1); sql = "select id,user_truename from l_user where id IN(" + sb.Length() + ")"; table = DbHelperSQL.Query(sql).Tables[0]; dlSelectUser.DataSource = table; dlSelectUser.DataBind(); }
[解决办法]
dlSelectUser.DataSource = table;
dlSelectUser.DataBind();
放在循环外面
[解决办法]
- C# code
string[] groupSub = groupList.Split(','); sql = string.Empty; DataTable table =new DataTable(); for (int i = 0; i < groupSub.Length; i++) { sql = "select id,user_truename from l_user where id='" + groupSub[i].ToString() + "'"; //有问题 table = DbHelperSQL.Query(sql).Tables[0]; ListItem lt = new ListItem(); lt.Text = table.Rows[0]["user_truename"].ToString(); lt.id = table.Rows[0]["id"].ToString(); dlSelectUser.Items.Add(lt); } dlSelectUser.DataBind();---这样做要和数据访问 好多次!建议, select id,user_truename from l_user where id IN()一次读出!在绑定!
[解决办法]
- C# code
string[] groupSub = groupList.Split(','); System.Text.StringBuilder sb = new System.Text.StringBuilder(); for (int i = 0; i < groupSub.Length; i++) { int n = 0; if (int.TryParse(groupSub[i], out n)) { sb.Append(groupSub[i] + ","); } } if (sb.Length > 0) { sb.Remove(sb.Length - 1, 1); sql = "select id,user_truename from l_user where id IN(" + sb.Length() + ")"; table = DbHelperSQL.Query(sql).Tables[0]; dlSelectUser.DataSource = table; dlSelectUser.DataBind(); }这个是一次,读出,直接绑定就行了!
[解决办法]
学习中
[解决办法]
[解决办法]
DataTable dt = new DataTable();
dt.Columns.Add("A");
dt.AcceptChanges();
for(int j=0;j<5;j++)
{
DataRow dr = dt.NewRow();
dr["A"] = "用户"+j;
dt.Rows.Add(dr);
}
CheckBoxList1.DataSource = dt;
CheckBoxList1.DataTextField = "A";
CheckBoxList1.DataValueField = "A";
CheckBoxList1.DataBind();
如果想显示多列的话用datalist,gridvie模板列加checkbox
[解决办法]
仔细看了下你的问题,你的sql有问题
是2楼所说的问题。
[解决办法]