在gridview中搜索复选框值

时间:2013-11-18 08:01:54

标签: c# asp.net .net ado.net

我在页面中有一个带有复选框的下拉列表,当我选择多个选项并搜索它时不返回网格中的所有选定值。它只返回一个选定的值。这是我用它进行搜索的代码。代码中的任何建议或更改。我已将一个gridview列绑定到该复选框。复选框位于gridview外部。代码中的任何建议或更改?

<asp:CheckBoxList ID="cblGroup" Style="vertical-align: baseline" runat="server" CssClass="chkbox">
</asp:CheckBoxList>

以下是按钮搜索的代码我试图获取复选框值

  protected void btnSearchGroup_Click(object sender, ImageClickEventArgs e)
        {
            SqlConnection con = new SqlConnection(strcon);
            SqlCommand cmd = new SqlCommand("select * from AppInvent_Test where Designation= '" + cblGroup.SelectedValue + "'", con);
            SqlDataAdapter Adpt = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            Adpt.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        } 

1 个答案:

答案 0 :(得分:2)

正如Sudhakar所提到的那样,试试这个

    protected void btnSearchGroup_Click(object sender, EventArgs e)
            {
                string selectedValues = string.Empty;

                foreach (ListItem item in cblGroup.Items)
                {
                    if (item.Selected)
                        selectedValues += "'" + item.Value + "',";
                }

                if (selectedValues != string.Empty)
                    selectedValues = selectedValues.Remove(selectedValues.Length - 1);//To remove the last comma;


                SqlConnection con = new SqlConnection(strcon);
                SqlCommand cmd = new SqlCommand("select * from AppInvent_Test where Designation in (" + selectedValues + ")", con);
                SqlDataAdapter Adpt = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                Adpt.Fill(dt);
                GridView1.DataSource = dt;
                GridView1.DataBind();

            }