Solved

Filtering CheckBoxList datasource problem

Posted on 2011-09-12
3
474 Views
Last Modified: 2012-05-12
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
Comment
Question by:Phosphor
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
ivan_vagunin earned 500 total points
ID: 36524542
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
 
LVL 1

Author Comment

by:Phosphor
ID: 36524992
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
 
LVL 1

Author Closing Comment

by:Phosphor
ID: 36525012
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now