0

您好先生,我想使用复选框 aur checkboxlist 搜索数据,例如如果我选择两个复选框,那么我想获取两个复选框 id 的数据,此代码一次只给我一个数据。请给我一个此类查询的演示代码。

私人无效复选框列表绑定(){

    SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\FlagBits\\Documents\\Visual Studio 2010\\WebSites\\checkboxlist\\App_Data\\Database.mdf;Integrated Security=True;User Instance=True");
    con.Open();
    string query = "select * from student where id='" + CheckBox1.Text + "'";
    SqlCommand cmd = new SqlCommand(query, con);
    SqlDataReader dr;
    dr = cmd.ExecuteReader();
    GridView1.DataSource = dr;
    GridView1.DataBind();



}
private void checkboxlistbind2()
{

    SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\FlagBits\\Documents\\Visual Studio 2010\\WebSites\\checkboxlist\\App_Data\\Database.mdf;Integrated Security=True;User Instance=True");
    con.Open();
    string query = "select * from student where id='" + CheckBox2.Text + "'";
    SqlCommand cmd = new SqlCommand(query, con);
    SqlDataReader dr;
    dr = cmd.ExecuteReader();
    GridView1.DataSource = dr;
    GridView1.DataBind();

}


protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{



    if (CheckBox1.Checked == true)
    {
        checkboxlistbind();
    }

}
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
    if (CheckBox2.Checked == true)
    {
        checkboxlistbind();
        checkboxlistbind2();
    }
4

1 回答 1

0

您需要遍历您的复选框列表并测试每个项目的选定值并构建一个包含每个选项的字符串。

    string queryparam = '';
      for (int i=0; i<checkboxlist1.Items.Count; i++) {
         if (checkboxlist1.Items[i].Selected)
            {  queryparam  += (queryparam.Length = 0) ? "id = " + checkboxlist1.Items[i].Text : " or id = "  checkboxlist1.Items[i].Text }
      }

这应该给你一个开始的想法。

于 2013-08-07T12:36:29.953 回答