如题所述
table1åtable2é½æ两个å段ID,dataã
以ä¸ä»£ç æ¯å°table1ç»å®å°CheckBoxListæ§ä»¶éã
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.CheckBoxListBind();
}
}
public void CheckBoxListBind()
{
SqlConnection con = DB.creatconnection();
con.Open();
SqlCommand cmd = new SqlCommand("select * from table1", con);
this.CheckBoxList1.DataTextField = "data";
this.CheckBoxList1.DataValueField = "ID";
this.CheckBoxList1.DataSource = cmd.ExecuteReader();
this.CheckBoxList1.DataBind();
}
以ä¸ä»£ç æ¯å°CheckBoxListæ§ä»¶æéçå¼æå ¥å°table2éã
protected void Button2_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.CheckBoxList1.Items.Count; i++)
{
if (this.CheckBoxList1.Items[i].Selected)
{
int j = i + 1;
string CheckBoxListData =Convert.ToString( this.CheckBoxList1.Items[i].Value);
SqlConnection con = DB.creatconnection();
con.Open();
SqlCommand cmd = new SqlCommand("insert into table2 (data)values('" + CheckBoxListData + "')", con);
cmd.ExecuteNonQuery();
Response.Write("æå ¥æåã");
}
}
}
å¦å¤,ç«é¿å¢ä¸æ产åå¢è´,便å®æä¿è¯
以ä¸ä»£ç æ¯å°table1ç»å®å°CheckBoxListæ§ä»¶éã
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.CheckBoxListBind();
}
}
public void CheckBoxListBind()
{
SqlConnection con = DB.creatconnection();
con.Open();
SqlCommand cmd = new SqlCommand("select * from table1", con);
this.CheckBoxList1.DataTextField = "data";
this.CheckBoxList1.DataValueField = "ID";
this.CheckBoxList1.DataSource = cmd.ExecuteReader();
this.CheckBoxList1.DataBind();
}
以ä¸ä»£ç æ¯å°CheckBoxListæ§ä»¶æéçå¼æå ¥å°table2éã
protected void Button2_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.CheckBoxList1.Items.Count; i++)
{
if (this.CheckBoxList1.Items[i].Selected)
{
int j = i + 1;
string CheckBoxListData =Convert.ToString( this.CheckBoxList1.Items[i].Value);
SqlConnection con = DB.creatconnection();
con.Open();
SqlCommand cmd = new SqlCommand("insert into table2 (data)values('" + CheckBoxListData + "')", con);
cmd.ExecuteNonQuery();
Response.Write("æå ¥æåã");
}
}
}
å¦å¤,ç«é¿å¢ä¸æ产åå¢è´,便å®æä¿è¯
温馨提示:答案为网友推荐,仅供参考
第1个回答 2011-05-05
selectvalue就是选中的值,然后通过你的添加方法加入就好了
第2个回答 2011-05-05
添加一个SQLDatasource然后配置数据源,然后在SQLDatasourse属性中的InsertQuery中写入相关插入SQL语句,语句中的要插入字段 字段=@字段,然后刷新参数,那个参数右边的参数源选择你你那个CheckboxList