Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Session Variable Question, asp c#

Posted on 2014-11-19
13
Medium Priority
?
284 Views
Last Modified: 2014-11-21
In a list box, after I click on First_Name, the following stores the text value (Angela in this example)  in a session variable dictionary object named settings:
    private void ListBoxTextChangedChanged(object sender, EventArgs e)
    {
        ListBox lb = (ListBox)sender;
        string name =lb.ID;
        name = name.Substring(3);
        int index = lb.SelectedIndex;
        settings[name] = lb.Text;
        Response.Write("1. " + settings["First_Name"].ToString());
        Session["MySettings"] = settings; 
    }

Open in new window

Soon after the click I get the following on the screen because of Response.Write(...) in these events:
2. All    //<-- All means no selection is made.
1. Angela

Please see below for Page_Load:
        if (IsPostBack)
        {
           settings = (Dictionary<string, string>)Session["MySettings"];
           Response.Write("2. " + settings["First_Name"].ToString());
            settings = HttpContext.Current.Session["MySettings"] as Dictionary<string, string>;
            AddListBoxes(listBoxes,"PstBack");
        }

Open in new window


Question: How can I change my code in Page_Load to have a print out like:
 
2. Angela
      //<-- why this still shows All
1. Angela
0
Comment
Question by:Mike Eghtebas
  • 6
  • 6
13 Comments
 
LVL 13

Expert Comment

by:Jitendra Patil
ID: 40454394
may be this is because you page is reloaded again on the selection changes in listbox, just try to change the below line of code
if (IsPostBack)   to if (!IsPostBack)

try the above one if it helps or  just post your markup code also.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40454507
Presumably it is printing out like that because of the sequence of events.
Page_Load fires before SelectedIndexChanged so it will always show the value from the dictionary as it was prior to the selection of a new item.
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40454514
I have selected "Angela" upon which Page_Load gets fired. Here I am expecting "Angela" to be printed not All. So, basically I think the problem is related to my session variable coding.

Is my session variable coding correct?

Mike
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40454530
As I said the reason it is behaving as it is is because of the sequence in which the events fire.
Your Session value isn't updated until the SelectedIndex event fires. But Page_Load fires before SelectedIndexChanged hence your Session variable hasn't been updated by the time you try to display the value.

So, assuming you have All in you dictionary, the sequence is currently:

1) You click "Angela" in the ListBox
2) The page posts back and Page_Load executes
3) Page_Load outputs "All" because that is what is currently in the Session
4) SelectedIndexChanged fires, which updates the Session with the value "Angela"

So, basically, Page_Load will always show the value that is in the Session before SelectedIndexChanged updates it.
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40454556
I think step 1a is missing:
1) You click "Angela" in the ListBox
1a) "Angela" enters into settings dictionary variable in the same ListBox event.
(And, settings ought to be available with "Agela" in it in postback = true of  Page_Load. But why it is not?)
2) The page posts back and Page_Load executes
3) Page_Load outputs "All" because that is what is currently in the Session
4) SelectedIndexChanged fires, which updates the Session with the value "Angela"
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40454578
Step 1A wasn't missing, that is the whole point.

"Angela" is put into the dictionary in the ListBox event, but that event fires after the Page_Load event. So the updated value isn't available in Page_Load.
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40454594
Does StateView remembers "Angela" was selected in Page_Load?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40454604
You can access the ListBox directly from Page_Load and retrieve the value that way.
i.e. In Page_Load:
ListBox lb = this.<name_of_your_listbox>;
string value = lb.Text;

Open in new window

0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40454670
1. User clicks on an option (presumably session variable of some kind ought to remember it).
2. Page_Load fires (because Postback is true, it is expected that the session variable to have the selected value).

If above statements are correct, please help me with its coding.

Thanks,

Mike
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40454772
>> If above statements are correct
The above statements aren't true.

Nothing gets put into the session automatically.  The data from the page is sent to the server as part of the Request object.  The page and its controls are then reconstructed based on the data sent from the client.

Next, the page initialization events fire, followed by Page Load, followed by any events for individual controls.  

Page load does not have the up-to-date session value because the session value is set by a Control event, which runs after the Load event
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40457875
Carl,

My apology for not following it; no doubt you are correct. What I want to give me something that works.

What I want is to click on the listbox and this action supplies my selection as a criteria to my rest of the code to filter accordingly.

Just to let you know, I have all code stuff taken care of. The problem I am having is I have to click on a "Angela", for example, but the screen refreshes without being filtered. However on a second click it does.

About second click, if I click again on "Angela", of course, the event will not fire and I have to click on any other item.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 40458248
If you are simply wanting to filter the data on the current page, then you don't need to use the Session at all.  You can simply grab the currently selected value from your list at any point on the page and use it to filter.

The listbox, and its selected valued, are passed back to the server each time the page is submitted. Therefore you don't need to store it in the Session, you can simply grab the value from the control when needed.

Something like:
private void Page_Load(object sender, EventArgs e)
{
     // grab the currently selected value from the listbox
     // assuming your list box is called "lstFirstName" - adjust to suit your code
     string selectedValue = this.lstFirstName.Text;
     
     FilterSomeData( selectedValue );
}

Open in new window

Hope that makes sense.
0
 
LVL 34

Author Closing Comment

by:Mike Eghtebas
ID: 40459033
Thank you for your patience.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

876 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