Solved

How to make a variable available within multiple events

Posted on 2009-06-29
6
273 Views
Last Modified: 2012-05-07
Hi All,

This is probably simple but the brain is really slow today.. blaming it on the heatwave!..

Within my code-behind I have a page_load and button_click event.

How can I declare a variable, assign a value to the variable within the PAGE_LOAD event and that value be available for me within the BUTTON_CLICK event?

many thanks,

Rit
protected void Page_Load(object sender, EventArgs e)

{

assign variable

}
 

protected void ButtonAddBasket_Click(object sender, ImageClickEventArgs e)

{

use variable value

}

Open in new window

0
Comment
Question by:rito1
  • 2
  • 2
  • 2
6 Comments
 
LVL 5

Accepted Solution

by:
johnaryan earned 250 total points
ID: 24734925
one option is to place it in the ViewState.

protected void Page_Load(object sender, EventArgs e)

{

ViewState["var1"] = "hello";

}

 

protected void ButtonAddBasket_Click(object sender, ImageClickEventArgs e)

{

String var1 = (string)ViewState["var1"];

}

Open in new window

0
 
LVL 4

Assisted Solution

by:vbgb
vbgb earned 250 total points
ID: 24734987
Another is to simply declare it as a module level variable. The page load event always fires before the button click on every postback.
private string var1="";
 

protected void Page_Load(object sender, EventArgs e)

{

var1 = "Hello World";

}

 

protected void ButtonAddBasket_Click(object sender, ImageClickEventArgs e)

{

String var2 = var1;

}

 

Open in new window

0
 
LVL 1

Author Comment

by:rito1
ID: 24735131
Thanks both. They both seem viable solutions.. is there much difference between the 2 or do you feel it is fair to split the points?

Rit
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 5

Expert Comment

by:johnaryan
ID: 24735431
Whichever you prefer.

vbgb's solution is the more correct method, if no posting back is involved.

My method assumed you needed to persist data between calls.
0
 
LVL 4

Expert Comment

by:vbgb
ID: 24735975
I am happy for you to split - although my method also works when posting back as in order to run the server side button click event, the page load happens as well, with Page.IsPostBack = True

This works for a static value that you wish to set in the load event and then use in the button click. If the button click value might need to be different you would need to use viewstate (either as John suggests or by persisting the value in a hidden field on the page) if the value may not always be the same each time

In the latter case you would only initialise the variable in the page load when Page.IsPostback is False. Then, if some other event changes it, it is not reset (in this case to 'Hello World') on every postback.


0
 
LVL 1

Author Closing Comment

by:rito1
ID: 31597852
HI Both, I have found both solutions extremely valuable for different situations now that I understand the differences so if you don't mind I have split it.

Many thanks both.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now