[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 496
  • Last Modified:

Filtering CheckBoxList datasource problem

Hello Experts,

I have a page where I have a CheckBoxList Control set to a default SelectCommand in my ASPX page, I am using a RadioButtonList to filter the return values by changing the SelectCommand on the SelectedIndexChanged event. In the code-behind, I am using it like so;
If RadioButtonList1.SelectedValue = 1 Then
            SqlDataSource1.SelectCommand = "SELECT [id], [Prod_Name]...
            SqlDataSource1.DataBind()
        ElseIf RadioButtonList1.SelectedValue = 2 Then
            SqlDataSource1.SelectCommand = "SELECT [id], [Prod_Name]...
            SqlDataSource1.DataBind()
        ElseIf RadioButtonList1.SelectedValue = 3 Then
            SqlDataSource1.SelectCommand = "SELECT [id], [Prod_Name]...
            SqlDataSource1.DataBind()...

End If

On a ButtonClick Event the CheckBoxList.SelectedItem.Text  is written to another table in the database.

My problem is although the filtering does work on the radiobutton selection (the CheckBoxList control values are filtered properly in both html return and source-view), the insert will always
use the default list that was originally loaded at initial runtime.
How do I correct this?

I have tried Dim CheckBoxList1 As New CheckBoxList in various places to no avail.

Thanks!
0
Phosphor
Asked:
Phosphor
  • 2
1 Solution
 
ivan_vaguninCommented:
Hi! You should save SelectCommand in ViewState - if you want to save filtering between postbacks and in SessionState if you want to save filtering all session long.

If RadioButtonList1.SelectedValue = 1 Then
            SqlDataSource1.SelectCommand = "SELECT [id], [Prod_Name]...
            SqlDataSource1.DataBind()
        ElseIf RadioButtonList1.SelectedValue = 2 Then
            SqlDataSource1.SelectCommand = "SELECT [id], [Prod_Name]...
            SqlDataSource1.DataBind()
        ElseIf RadioButtonList1.SelectedValue = 3 Then
            SqlDataSource1.SelectCommand = "SELECT [id], [Prod_Name]...
            SqlDataSource1.DataBind()...
End If
ViewState("MySelectCommand") = SqlDataSource1.SelectCommand

then you should init SqlDataSource1.SelectCommand in OnLoad Event (indeed OnLoad fires before event handlers, so if user changes the selection SelectCommand will be overwritten)
Protected Sub OnLoad(ByVal e As EventArgs)
     SqlDataSource1.SelectCommand = CType(ViewState("MySelectCommand"),String)
End Sub

0
 
PhosphorAuthor Commented:
Actually what ended up working was changing the checkboxlist viewstate property from false to true.

Thank you for your response as it did eventually lead to resolution.
0
 
PhosphorAuthor Commented:
It's working now as intended and your answer got me to thinking about viewstate in general which I hadn't yet considered.

Thanks again.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now