?
Solved

How to make a variable available within multiple events

Posted on 2009-06-29
6
Medium Priority
?
280 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
[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
  • 2
  • 2
  • 2
6 Comments
 
LVL 5

Accepted Solution

by:
johnaryan earned 1000 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 1000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

771 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