Solved

Filtering CheckBoxList datasource problem

Posted on 2011-09-12
3
483 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

696 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