Databinding RRS feed

  • Question

  • User-401818107 posted

    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 you!

    Sub Check_Clicked(sender As Object, e As System.EventArgs)
    If checkbox1.Checked = true then
    Dim strSQL
     strSQL = "SELECT * FROM products where xxx "  
    call openDB()	
    					dg1.dataSource = DBDataSet1
    		call CloseDB()		
    		end if
    End sub	
     Sub Check1_Clicked(sender As Object, e As System.EventArgs)	
    If checkbox2.Checked = true then
    Dim strSQL1
    strSQL1 = "SELECT * FROM products where xxx  "  
    call openDB()	
    					dg2.dataSource = DBDataSet2

    Thursday, December 27, 2018 3:55 AM

All replies

  • User-893317190 posted

    Hi Jessy,

    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>

    Code behind.

      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
            End If
            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
            End Using
        End Sub

    The result.

    If it is not your case , please post  more of your code.

    Best regards,

    Ackerly Xu

    Thursday, December 27, 2018 8:09 AM
  • User-401818107 posted

    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?

    Thank you!

    Thursday, December 27, 2018 8:16 PM
  • User-893317190 posted

    Hi Jessy,

    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.


    Best regards,

    Ackerly Xu

    Friday, December 28, 2018 12:58 AM