Solved

How to make a variable available within multiple events

Posted on 2009-06-29
6
278 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 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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.…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

738 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