Wrox Programmer Forums
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 29th, 2010, 10:32 AM
Authorized User
 
Join Date: Mar 2010
Posts: 27
Thanks: 2
Thanked 1 Time in 1 Post
Default global session

I create session in Page load like:
Code:
protected void Page_Load(object sender, EventArgs e)
    {
        Session["text"] = Request.Form["text_ad"];
    }
and I need access to this session in the same page in Button1_Click like:
Code:
protected void Button1_Click(object sender, EventArgs e)
    {
       Response.Write(Session["text"]);
    }
But I cant access to the session.
 
Old March 29th, 2010, 10:51 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Well what you have written would certainly work. However you are aware that Page_Load is called every time the page loads, including when a button is pressed. So its quite possible you are writing an empty string back into the session variable.

Use Page.IsPostBack to see if you are posting back or not, or do a check to see if Session has already been set perhaps?
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old March 29th, 2010, 11:04 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Seems a strange use case, given that if you have a form parameter that you retrieve on Page_Load you can also retrieve it in the click handler.
Anyway, the Session variable will be set every time the page loads and this occurs before the click handler, are you certain the text_ad is being sent every time?

Perhaps you need to only set the Session variable if it's not a postback?
__________________
Joe
http://joe.fawcett.name/
 
Old March 29th, 2010, 11:22 AM
Authorized User
 
Join Date: Mar 2010
Posts: 27
Thanks: 2
Thanked 1 Time in 1 Post
Default access to session

I dont understand what do you mean.
the session stored in Page_Load ()
and I need to access to this session when the Button1_Click() is clicked.
the Page_Load () and Button1_Click()
in the same page
 
Old March 29th, 2010, 11:29 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

When you first load the page, Page_Load is called.

Then when you press the button it reloads the page (called a 'PostBack').

This calls Page_Load, and then calls Button1_Click.

When Page_Load is called the first time I suspect Request.Form["text_ad"] returns something, and stores that in the session variable.

The second time however I suspect that Request.Form["text_ad"] will be empty, so overwrite the previously stored value in the session variable with an empty string.

Simples?
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Global.Asax Session_End and Session.SessionID MissHenesy ASP.NET 2.0 Professional 7 November 3rd, 2009 01:20 AM
Session cookies in global.asa mabelis Classic ASP Professional 7 August 4th, 2007 07:48 AM
Session States, Global. asax and web.config ohmbhu General .NET 2 July 1st, 2004 09:15 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.