Как отображать результаты сразу после установки флажка - PullRequest
0 голосов
/ 30 октября 2019

Я использую ASP.net GridView. Есть флажки, которые фильтруют результаты после выбора флажка и нажатия кнопки поиска, но я хочу удалить кнопку для автоматической фильтрации после выбора флажков.

Содержимое, отображаемое в виде таблицы, из таблицы базы данных.

ASP

<b><label style="font-size:25px">Brand</label></b>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="SBrand" DataValueField="SBrand">

</asp:CheckBoxList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT DISTINCT [SBrand] FROM [Stock]"></asp:SqlDataSource>

C #

protected void gvStock_SelectedIndexChanged(object sender, EventArgs e)
        {
            string id = gvStock.SelectedRow.Cells[0].Text;
            Response.Redirect("Details.aspx?ID=" + id);
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string chkbox = "";
            Label1.Visible = false;
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                {
                    if (chkbox == "")
                    {
                        chkbox = "'" + CheckBoxList1.Items[i].Text + "'";
                    }
                    else
                    {
                        chkbox += "," + "'" + CheckBoxList1.Items[i].Text + "'";
                    }
                    Label1.Text = chkbox;

                    string mainconn = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
                    SqlConnection sqlconn = new SqlConnection(mainconn);
                    string sqlquery = "SELECT [pCode],[pID],[bCode], [SBrand], [SDescription], [sCost] , [sPrice] , [SType] , [sSupplierName] , [sSupplierDirect] FROM Stock where SBrand in (" + Label1.Text + ")";
                    SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
                    sqlconn.Open();
                    SqlDataAdapter sda = new SqlDataAdapter(sqlcomm);
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    this.gvStock.DataSource = dt;
                    this.gvStock.DataBind();

                }
            }
        }

1 Ответ

0 голосов
/ 30 октября 2019

ASPX

 <asp:CheckBoxList ID="CheckBoxList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="SBrand" DataValueField="SBrand" AutoPostBack="True" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged">

.CS

 protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string chkbox = "";
        Label1.Visible = false;
        for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        {
            if (CheckBoxList1.Items[i].Selected)
            {
                if (chkbox == "")
                {
                    chkbox = "'" + CheckBoxList1.Items[i].Text + "'";
                }
                else
                {
                    chkbox += "," + "'" + CheckBoxList1.Items[i].Text + "'";
                }
                Label1.Text = chkbox;

                string mainconn = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
                SqlConnection sqlconn = new SqlConnection(mainconn);
                string sqlquery = "SELECT [pCode],[pID],[bCode], [SBrand], [SDescription], [sCost] , [sPrice] , [SType] , [sSupplierName] , [sSupplierDirect] FROM Stock where SBrand in (" + Label1.Text + ")";
                SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
                sqlconn.Open();
                SqlDataAdapter sda = new SqlDataAdapter(sqlcomm);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                this.gvStock.DataSource = dt;
                this.gvStock.DataBind();

            }
        }
    }
...