【问题标题】:Search checkbox values in gridview在gridview中搜索复选框值
【发布时间】:2013-11-19 03:09:15
【问题描述】:

我在页面中有一个带有复选框的下拉列表,当我选择多个选项并搜索它时,它不会返回网格中的所有选定值。它只返回一个选定的值。这是我用于搜索的代码。代码中的任何建议或更改。我已将一个 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();
        } 

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 一切都很完美,但我认为您需要为每个选定项目添加单引号:) -> selectedValues += "'"+item.Value+"',";并且您可以在此处删除单引号=> 在 (" + selectedValues + ")" 中的指定位置
    • Akbar Ali 应得的,他为您制定了可行的解决方案。我对您的谦虚要求是“接受它作为答案”
    • 对不起,伙计们。我是这个论坛的新手,已经习惯了:) 感谢 Akbar 和 sudhakar。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    相关资源
    最近更新 更多