Last post Dec 28, 2018 12:58 AM by Ackerly Xu
Dec 27, 2018 03:55 AM|Jessy|LINK
I have several check boxes. When checkbox1 is selected, it retrieves the data based on the condition checkbox1. When checkbox2 is selected, it retrieves the data based on the condition checkbox2.
In my current situation, I have to call each time for the check box. Is there any way to bind the data only once for all of the check boxes instead of one by one. Here is the code for checkbox1 and checkbox2. Please advise a simple and better way. Thank
Sub Check_Clicked(sender As Object, e As System.EventArgs)
If checkbox1.Checked = true then
strSQL = "SELECT * FROM products where xxx "
dg1.dataSource = DBDataSet1
Sub Check1_Clicked(sender As Object, e As System.EventArgs)
If checkbox2.Checked = true then
strSQL1 = "SELECT * FROM products where xxx "
dg2.dataSource = DBDataSet2
Dec 27, 2018 08:09 AM|Ackerly Xu|LINK
Not clear about what the design of your aspx is, it seems you want to only write one method.
If so , you could try the code below. My design is when user check the checkbox , corresponding products of category the checkbox represents will be bound to the gridview.
<form id="form1" runat="server">
<%-- store categoryid in a customized property --%>
Cars: <asp:CheckBox ID="CheckBox1" param="1" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged" />
Planes:<asp:CheckBox ID="CheckBox2" param="2" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged" />
Trucks<asp:CheckBox ID="CheckBox3" param="3" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged" />
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
Private constr As String = ConfigurationManager.ConnectionStrings("WingtipToysConnectionString").ConnectionString
Protected Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs)
Dim check As CheckBox = CType(sender, CheckBox) 'get the current checkbox
If Not check.Checked Then
Return 'check whether it is checked
Using adapter As SqlDataAdapter = New SqlDataAdapter("select * from products where CategoryiD =@ID", constr)
adapter.SelectCommand.Parameters.AddWithValue("ID", check.Attributes("param")) ' get the id stored in the checkbox
Dim table As DataTable = New DataTable()
GridView1.DataSource = table
If it is not your case , please post more of your code.
Dec 27, 2018 08:16 PM|Jessy|LINK
Thank you Ackerly!
In the sql statement, I need several of them since I need to select those from the pricing table. The pricing will range from $10, $20 etc.
BTW, could you convert the C# into VB?
Dec 28, 2018 12:58 AM|Ackerly Xu|LINK
If you want to bind two two params, you could store two arguments in the checkbox(if all your sql has two parameters.)
Cars: <asp:CheckBox ID="CheckBox1" param="1,2" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged" />
Then you could use string.split method to get 1 and 2 and insert into the sql.
I write the code with VB, you could visit the website below to convert vb to c# or c# to vb.