473,406 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Button_Click and Session

Hi,

Sorry i'm a newbie with Asp.net, here is my problem :

private void Page_Load(object sender, System.EventArgs e)
{
if (IsPostBack) { Label1.Text = (string)Session["test"]; }
}

private void Button1_Click(object sender, System.EventArgs e)
{
Session["test"] = "test";
}

I have to click two times on the button to see "test".

Thanks for any help.
Nov 11 '07 #1
8 2615
re:
!I have to click two times on the button to see "test".

try this :

private void Button1_Click(object sender, System.EventArgs e)
{
Session["test"] = "test";
Label1.Text = (string)Session["test"];
}

Or...you can try this :

private void Page_Load(object sender, System.EventArgs e)
{
Label1.Text = (string)Session["test"];
}


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espańol : http://asp.net.do/foros/
======================================
"Marine" <no**@none.orgwrote in message news:%2****************@TK2MSFTNGP04.phx.gbl...
Hi,

Sorry i'm a newbie with Asp.net, here is my problem :

private void Page_Load(object sender, System.EventArgs e)
{
if (IsPostBack) { Label1.Text = (string)Session["test"]; }
}

private void Button1_Click(object sender, System.EventArgs e)
{
Session["test"] = "test";
}

I have to click two times on the button to see "test".

Thanks for any help.

Nov 11 '07 #2
Hi Marine,

Marine schrieb:
Hi,

Sorry i'm a newbie with Asp.net, here is my problem :
I have to click two times on the button to see "test".
The reason is that Page_Load gets executed before Button1_Click, so when
you click the button for the first time, Page_Load sets the label to an
(empty) session variable and only after that the session variable is set
to the string "test".

As Juan has shown, you can set the label text in the click event
directly and don't need to do that in Page_Load at all.

Hope this helps,

Roland
Nov 11 '07 #3
the first, you save the session, but the Label1.text was set before the
session was saved.

The second click, the session is there already, so it is set.

regards,

Guoqi Zheng
http://www.ureader.com

url:http://www.ureader.com/msg/141730872.aspx
Nov 11 '07 #4
because Page_Load comes ealier than Button1_Click.

may it help~

Ryan
Marine wrote:
Hi,

Sorry i'm a newbie with Asp.net, here is my problem :

private void Page_Load(object sender, System.EventArgs e)
{
if (IsPostBack) { Label1.Text = (string)Session["test"]; }
}

private void Button1_Click(object sender, System.EventArgs e)
{
Session["test"] = "test";
}

I have to click two times on the button to see "test".

Thanks for any help.
Nov 12 '07 #5
change the
if (IsPostBack)

to

if (!IsPostBack)

:)

On Sun, 11 Nov 2007 19:19:58 +0100, "Marine" <no**@none.orgwrote:
>Hi,

Sorry i'm a newbie with Asp.net, here is my problem :

private void Page_Load(object sender, System.EventArgs e)
{
if (IsPostBack) { Label1.Text = (string)Session["test"]; }
}

private void Button1_Click(object sender, System.EventArgs e)
{
Session["test"] = "test";
}

I have to click two times on the button to see "test".

Thanks for any help.
Nov 12 '07 #6
John MJ Gorter wrote:
change the
if (IsPostBack)

to

if (!IsPostBack)

:)
That will have the opposite effect of the wanted. With your suggested
chage, the text will never change.

--
Göran Andersson
_____
http://www.guffa.com
Nov 12 '07 #7
Oops, misread the intention, sorry...

Button_Click gets handled after the page_load, hence the label get's
set to early... Setting the label in the Button_Click should help...
but that was posted as an option earlier this thread :-)

On Mon, 12 Nov 2007 09:26:49 +0100, Göran Andersson <gu***@guffa.com>
wrote:
>John MJ Gorter wrote:
>change the
if (IsPostBack)

to

if (!IsPostBack)

:)

That will have the opposite effect of the wanted. With your suggested
chage, the text will never change.
Nov 12 '07 #8
on 11-11-2007, Marine supposed :
Hi,

Sorry i'm a newbie with Asp.net, here is my problem :

private void Page_Load(object sender, System.EventArgs e)
{
if (IsPostBack) { Label1.Text = (string)Session["test"]; }
}

private void Button1_Click(object sender, System.EventArgs e)
{
Session["test"] = "test";
}

I have to click two times on the button to see "test".

Thanks for any help.
And a suggestion I've not yet seen in this thread:
move the "label setting" from Page_Load to Page_PreRender, which is
called *after* the click has been handled.

Hans Kesting
Nov 12 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Paul Aspinall | last post by:
Hi I have a form with a button on it. It seems that when I click the button, it causes Form_load to fire, before the Button_Click procedure Does anyone know why?? Thanks
3
by: Tim Thomas | last post by:
Hi, I am very new to .NET and am in the process of building my first web application. I will briefly describe what i am trying to achieve: I have a system where suppliers register their...
2
by: Frank Schumacher | last post by:
Hi Folks, I have a problem with the order of events fired by ASP.NET. I found many articles which explaining the lifecycle of a site, but I found none which took the event from a Control on the...
3
by: I am Sam | last post by:
Please help. I'm dying here and pulling out the last few remaining elements of my hair over this. I have built a form that will identify and authenticate users. I keep getting the following...
9
by: gaddoznntp | last post by:
Hi all, I've a server button handling a button_click procedure. If the client user clicks the button, the page causes the postback and the button_click procedure is fired. Then, if the user...
13
by: Matt | last post by:
I am trying to call a recursive subroutine within a Button_Click event. How can I do this? I tried to create a seperate Function() but it will not allow me to use any variables that I created in...
2
by: VMI | last post by:
I have a LinkButton_search on my Page1.aspx that opens up a popup page called popup.aspx. I do this with LinkButton.Attributes.Add() on the Page_Load of Page1.aspx. How can I add server-side code...
0
by: John | last post by:
I am loading my controls dynamically into my asp.net page. When I click on a button on one of those user controls then page reloads and I need to reload the page based on what has happened in the...
2
by: Jason Huang | last post by:
Hi, In my .Net 2.0 C# web form Form1, say I have a button btnToForm2. Is the Request.Redirect("Form2") the right way to go to the Form2 in the btnToForm2_Click event? Actually, I also want to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.