如何在选中复选框后立即显示结果
How to display results immediately after selecting checkbox
我正在使用 ASP.net GridView。有复选框在选中复选框并单击搜索按钮后过滤结果,但我想删除按钮以在选中复选框后自动过滤。
gridview中显示的内容来自数据库table
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();
}
}
}
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();
}
}
}
我正在使用 ASP.net GridView。有复选框在选中复选框并单击搜索按钮后过滤结果,但我想删除按钮以在选中复选框后自动过滤。
gridview中显示的内容来自数据库table
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();
}
}
}
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();
}
}
}