
C#中checklistbox的用法
有控件checklistbox(项目一,项目二。。。)和表:项目表(项目名称,项目编号),员工项目表(项目编号,员工编号)。其中项目编号,(项目编号,员工编号)是主键。现...
有控件checklistbox(项目一,项目二。。。)和表:项目表(项目名称,项目编号),员工项目表(项目编号,员工编号)。其中项目编号,(项目编号,员工编号)是主键。
现在如何遍历checklistbox中勾中的项到数据库(项目表)取出项目编号,存储到员工项目表中,其中员工编号为textbook中的值。 展开
现在如何遍历checklistbox中勾中的项到数据库(项目表)取出项目编号,存储到员工项目表中,其中员工编号为textbook中的值。 展开
展开全部
前台:
<div>
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
</asp:CheckBoxList>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
后台:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindCKBoxList();
}
}
static SqlConnection conn = new SqlConnection(@"server=;database=;uid=;pwd=;");
private void BindCKBoxList()
{
SqlDataAdapter dapt = new SqlDataAdapter(@"select * from item",conn);
DataTable dt = new DataTable();
dapt.Fill(dt);
CheckBoxList1.DataTextField = "name";//项目名称
CheckBoxList1.DataValueField = "id";//项目编号
CheckBoxList1.DataSource = dt;
CheckBoxList1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand comm=new SqlCommand();
comm.Connection = conn;
SqlParameter[] sp={new SqlParameter("@itemid",SqlDbType.Int),
new SqlParameter("@staffid",SqlDbType.VarChar)};
conn.Open();
for (int i=0;i<CheckBoxList1.Items.Count;i++)//遍历CheckBoxList
{
if (CheckBoxList1.Items[i].Selected)
{
comm.Parameters.Clear();
comm.CommandText=@"insert into staff values(@itemid,@staffid)";
sp[0].Value=CheckBoxList1.Items[i].Value.ToString();
sp[1].Value=TextBox1.Text.Trim();
comm.Parameters.AddRange(sp);
comm.ExecuteNonQuery();
}
}
conn.Close();
}
<div>
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
</asp:CheckBoxList>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
后台:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindCKBoxList();
}
}
static SqlConnection conn = new SqlConnection(@"server=;database=;uid=;pwd=;");
private void BindCKBoxList()
{
SqlDataAdapter dapt = new SqlDataAdapter(@"select * from item",conn);
DataTable dt = new DataTable();
dapt.Fill(dt);
CheckBoxList1.DataTextField = "name";//项目名称
CheckBoxList1.DataValueField = "id";//项目编号
CheckBoxList1.DataSource = dt;
CheckBoxList1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand comm=new SqlCommand();
comm.Connection = conn;
SqlParameter[] sp={new SqlParameter("@itemid",SqlDbType.Int),
new SqlParameter("@staffid",SqlDbType.VarChar)};
conn.Open();
for (int i=0;i<CheckBoxList1.Items.Count;i++)//遍历CheckBoxList
{
if (CheckBoxList1.Items[i].Selected)
{
comm.Parameters.Clear();
comm.CommandText=@"insert into staff values(@itemid,@staffid)";
sp[0].Value=CheckBoxList1.Items[i].Value.ToString();
sp[1].Value=TextBox1.Text.Trim();
comm.Parameters.AddRange(sp);
comm.ExecuteNonQuery();
}
}
conn.Close();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询