Solved

Filtering CheckBoxList datasource problem

Posted on 2011-09-12
3
480 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

786 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