如何将checkbox得到的N条数据插入到新的数据表?

来源:百度知道 编辑:UC知道 时间:2024/06/06 16:04:22
我现在是在datalist中套了checkbox 出现了N条数据
现在想当按钮发生事件后 选中的checkbox会插入新的表。
以下是代码,插入数据那不知道该怎么写。

protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
SqlConnection con = new SqlConnection("server=127.0.0.1;database=hbusm;uid=sa;pwd=sa;");
DataSet ds = new DataSet();
SqlDataAdapter dianming = new SqlDataAdapter("select * from lhziliao order by id asc", con);

dianming.Fill(ds, "lhziliao");
DataList1.DataSource = ds.Tables["lhziliao"].DefaultView;
DataList1.DataBind();

}}
protected void Button1_Click(object sender, EventArgs e)
{
string num = "";
foreach (Control c in DataList1.Controls)
{
CheckBox chk = (CheckBox)c.FindControl("CheckBox1");
if (chk.Checked)

你会查找DATALIST控件的CHECKBOX里的值,写入还难吗?
比如你有CheckBox1和CheckBox2.
for (int i = 0; i < DataList1.Items.Count;i++ )
{
String str,str1;
str= (CheckBox)(DataList1.Items[i].FindControl("CheckBox1")).Text.ToString();
str2=(CheckBox)(DataList1.Items[i].FindControl("CheckBox2")).Text.ToString();
SqlCommand cmd=new SqlCommand("insert 表(字段1,字段2) values('"+str1+"','"+str2+"')",con);
cmd.Open();
cmd.ExecuteNonQuery();
//doanything you want to do;

}