Solved

Preserve checkboxlist using session

Posted on 2012-04-02
3
586 Views
Last Modified: 2012-04-03
Been working on this for 4 hours now!

1. I have a checkboxList on page xyz.aspx
2. There's a seach link on this page. User clicks it, goes to search.aspx. Selects a row. I pass the id to xyz.aspx page
3. I use this ID and call the database, get a value and bind to checkboxList. So far so good.
4. But now, user might leave this page again and go to search and get another id. Now, I have 2 ids.
5. When I get back to page xyz.aspx, I want to bind both Ids to the checkboxlist. I'm thinking about using a session var and loop thru the session and bind bit not sure how to do this.

This is my code:

 if (Session["providerId"] != null)
        {

            List<usp_LoadProviderNameResult> provider = sd.LoadProvider(ReferredToProviderId);

            ckProviders.DataSource = provider;
            ckProviders.DataTextField = "Providername";
            ckProviders.DataValueField = "Id";
            ckProviders.DataBind();
        }
        List<string> selectedProviders = new List<string>();
        selectedProviders.Add(ReferredToProviderId.ToString());
        Session["providerId"] = selectedProviders;

Open in new window

0
Comment
Question by:Camillia
[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
3 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 250 total points
ID: 37799452
0
 
LVL 7

Assisted Solution

by:dhawalseth
dhawalseth earned 250 total points
ID: 37799895
Hi Farzadw,

You are pretty near to what you want to achieve. I believe the code you wrote is a complicated one and you can replace it with string[] object with ',' or ';' as delimiter to separate two entries.
There are a few things which i would like to point out.
1. By step 3 you mean that you pick a value from the database based on the passed id, and then select the value in checkboxlist.(correct me if i am wrong)
2. The list<string> object works fine, but if there is an existing entry in your check box list- it will not be preserved.
Possible solution: do this functionality when you are navigating to new page (using a button click event will do)
List<string> selectedProviders = new List<string>();
        //selectedProviders.Add(ReferredToProviderId.ToString());
replace this line with:
if(ckProviders.Items.Count > 0)
for(int var =0; var > ckProviders.Items.Count; var++)
{
if(ckProviders.Items[var].Selected)
{
selectedProviders.Add(value you want to add);
}
}
  Session["providerId"] = selectedProviders;
On page load do a reverse after you have bind your checkbox list.
if(Session["providerId"] != null)
List<string> selectedProviders = Session["providerId"];//Please convert this value back to List<string> type.
            foreach (string providerId in selectedProviders)
            {
                ListItem item = ckProviders.Items.FindByValue(providerId);
                if (item != null)
                    item.Selected = true;
            }

If you want to play with an string[] object follow the link shared above, works like a charm and a very good example is posted.

Mark as answer if helped. You might need to fix here and there if required in the code.
Enjoy!!
0
 
LVL 7

Author Comment

by:Camillia
ID: 37800624
let me review, thanks
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Save ms data to server side. 19 59
C# LINQ ForEach() question 6 77
Error running webpage local computer 7 26
Code works but breaks when I add one section 4 27
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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 …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

740 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