【问题标题】:binding the data in datalist绑定datalist中的数据
【发布时间】:2012-06-25 19:07:52
【问题描述】:

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

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();

        }
    }

【问题讨论】:

  • 仅最后一条记录绑定..如何绑定datalist中的更多记录
  • 如何使用此代码在 datlist 中绑定多行\
  • jadarnel27//// 你能解决这个问题吗?
  • 您是否将复选框的自动回发属性设置为 true?
  • 是的不是问题...我的问题是如何添加多个记录绑定数据列表

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


【解决方案1】:

您的绑定数据列表仅在 foreach 循环中。这就是为什么它每次在数据列表中显示一条记录。使数据列表绑定出 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.
           ...
        }
    }

现在使用查询将数据绑定到数据列表

 select * from <tablename> or where check=true

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

【讨论】:

    猜你喜欢
    • 2011-04-30
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    • 2011-10-27
    • 2023-03-03
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    相关资源
    最近更新 更多