asp.net中从数据库中读取数据,有对应的吧checkbox选中

来源:互联网 发布:数据库如何建立索引 编辑:程序博客网 时间:2024/04/29 05:18

在做一个管理页面时,首先需要把早,中,晚不同班次的复选框选中插入到数据库,然后还要在数据库中把对应的班次信息读出来,与对应的班次的checkbox选中

首先前台代码是这样的:

<td   ><asp:CheckBox ID="CheckBox12" runat="server" Text="早" /></td><td  ><asp:CheckBox ID="CheckBox13" runat="server"  Text="中"                     /></td><td  ><asp:CheckBox ID="CheckBox14" runat="server" Text="夜"                      /></td>

后台代码首先从数据库中读入

 MySqlCommand com = con.CreateCommand();com.CommandText = "select bc  from product where   pro_id=123 ";            MySqlDataAdapter ad = new MySqlDataAdapter(com);            DataTable d = new DataTable();            ad.Fill(d);            if (d.Rows.Count > 0)            {                CheckBox[] abc = { CheckBox12, CheckBox13, CheckBox14 };                string checkboxstr = "";                for (int i = 0; i < abc.Length; i++)                {                    checkboxstr = abc[i].Text;                    if (d.Rows[0]["bc"].ToString() == checkboxstr)//判断当前的text是否和数据库中相等                    {                        abc[i].Checked = true;                    }                }                            }


0 0
原创粉丝点击