绑定datalist中的数据

时间:2012-06-25 19:07:52

标签: asp.net ajax web-services c#-4.0

我想在datalist中绑定选定的复选框行..使用此代码但不绑定所有记录..

System.Web.UI.WebControls.CheckBox chkb = new System.Web.UI.WebControls.CheckBox();
string chkBoxIndex = string.Empty;

foreach (C1.Web.Wijmo.Controls.C1GridView.C1GridViewRow row in C1GridView1_listview.Rows)
{
    chkBoxIndex = (string)C1GridView1_listview.DataKeys[row.RowIndex].Value.ToString();
    chkb = (System.Web.UI.WebControls.CheckBox)row.FindControl("chkBxSelect");

    if (chkb.Checked == true)
    {

        cmd = new SqlCommand("select sample_code,convert(varchar(10),purchase_date,101) as purchase_date,sample_image_id,sample,image,style,descript from sample_shopping,sample_image where sample_shopping.sample_code=sample_image.sample and type='IMG' and sample_code='" + chkBoxIndex + "'", con);
        con.Open();
        SqlDataReader dr = cmd.ExecuteReader();

        if (dr.HasRows)
        {
            dr.Close();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);

            dl_search.DataSource = dt;
            dl_search.DataBind();
            con.Close();

        }
    }

1 个答案:

答案 0 :(得分:0)

仅在foreach循环中绑定datalist。这就是每次在datalist中显示一条记录的原因。使datalist绑定出foreach循环,如

System.Web.UI.WebControls.CheckBox chkb = new System.Web.UI.WebControls.CheckBox();
string chkBoxIndex = string.Empty;

    foreach (C1.Web.Wijmo.Controls.C1GridView.C1GridViewRow row in C1GridView1_listview.Rows)
    {
        chkBoxIndex = (string)C1GridView1_listview.DataKeys[row.RowIndex].Value.ToString();
        chkb = (System.Web.UI.WebControls.CheckBox)row.FindControl("chkBxSelect");

        if (chkb.Checked == true)
        {
          //update the table with update query like
           //update <table-name> set check='true' where sno=chkBoxIndex or insert the select records into seperate table.
           ...
        }
    }

现在使用类似

的查询将数据绑定到datalist
 select * from <tablename> or where check=true

这样只有选定的行才会显示在datalist中。希望这会有所帮助。