473,385 Members | 1,570 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,385 software developers and data experts.

Page variable

Hello!

I Have a problem. I try to use asp.net page-wide variable but it is not
working. I declare my boolean variable (eg. bool done=false) in the same
place where page's web controls are declared. However when I try to use it in
a function, like

if (this.done==false) {
Response.Write("writing file")
this.WriteFile();
this.done=true;
}

But everytime when i use this function again after postback variable "done"
is set to "false" and WriteFile function is called again althought I set it
to true.

Is this because asp.net is stateless? Can I get around this using ViewState
to store variable's value? If ViewState can be used, could someone write a
small example how should it be used? The help is very much appreciated.

Thanks in advance.

Peter

Nov 18 '05 #1
5 2508
"=?Utf-8?B?UGV0ZXI=?=" <Pe***@discussions.microsoft.com> wrote in
news:4F**********************************@microsof t.com:
But everytime when i use this function again after postback variable
"done"


Page Level variables are not persistent.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 18 '05 #2
Peter,

Instance of the current page is created with every request and is destroyed
at the end of the response.
If you need to store the data for future reference you should consider using
ViewSatte
This is how you write to write state

if you variable was
bool done = false; at the start

you set it it
done = true;
on a certain event handling
then you use it to viewstate like this

ViewState["ActionComplete"] = this.done;

you can read it in page load using something like

if(ViewState["ActionComplete"] != null)
{
this.done = (bool)ViewState["ActionComplete"];
}

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Peter" <Pe***@discussions.microsoft.com> wrote in message
news:4F**********************************@microsof t.com...
Hello!

I Have a problem. I try to use asp.net page-wide variable but it is not
working. I declare my boolean variable (eg. bool done=false) in the same
place where page's web controls are declared. However when I try to use it
in
a function, like

if (this.done==false) {
Response.Write("writing file")
this.WriteFile();
this.done=true;
}

But everytime when i use this function again after postback variable
"done"
is set to "false" and WriteFile function is called again althought I set
it
to true.

Is this because asp.net is stateless? Can I get around this using
ViewState
to store variable's value? If ViewState can be used, could someone write a
small example how should it be used? The help is very much appreciated.

Thanks in advance.

Peter

Nov 18 '05 #3

Hallo Peter,

Yes, ASP.NET is by its nature, stateless. However, you can utilize state
with using Viewstate, Session state, Application State and caching. In your
case, what you need seems to be the Viewstate, which is a page level storage
rendered as a hidden field on the page. Whatever you write into Viewstate is
encrypted as base64 string and added to the HTML so you should be careful.
It's tamperable and it increases the page size.But for this case since you
put only a simple boolean variable, it's fine. Try the code below; I wrote it
on the fly so fill the gabs in.

private const string VIEWSTATE_DONE = "done";
private bool done = false;

private void Page_Load(...)
{
this.PreRender += new ....; //Set the PreRender event.
if(!this.IsPostBack)
{
done = (bool) this.ViewState[VIEWSTATE_DONE];
}
}

private void PreRender(....)
{
//our job with the variable is finished. Write it back to viewstate.
this.ViewState[VIEWSTATE_DONE] = done;
}

Don't forget to check out the alternative ways of storage I mentioned above.

Hope this helps,

Ethem

"Peter" wrote:
Hello!

I Have a problem. I try to use asp.net page-wide variable but it is not
working. I declare my boolean variable (eg. bool done=false) in the same
place where page's web controls are declared. However when I try to use it in
a function, like

if (this.done==false) {
Response.Write("writing file")
this.WriteFile();
this.done=true;
}

But everytime when i use this function again after postback variable "done"
is set to "false" and WriteFile function is called again althought I set it
to true.

Is this because asp.net is stateless? Can I get around this using ViewState
to store variable's value? If ViewState can be used, could someone write a
small example how should it be used? The help is very much appreciated.

Thanks in advance.

Peter

Nov 18 '05 #4
Hi,

This is the common way of storing a variable in the viewstate:

public SomeType PropertyName
{
get
{
object a = ViewState["PropertyName"];
if(a != null)
return (SomeType)a;
return some_default_value; // or null when possible
}
set{ViewState["PropertyName"] = value;}
}

The property should be member of the page class, of course.

Hope this helps
Martin
"Peter" <Pe***@discussions.microsoft.com> wrote in message
news:4F**********************************@microsof t.com...
Hello!

I Have a problem. I try to use asp.net page-wide variable but it is not
working. I declare my boolean variable (eg. bool done=false) in the same
place where page's web controls are declared. However when I try to use it in a function, like

if (this.done==false) {
Response.Write("writing file")
this.WriteFile();
this.done=true;
}

But everytime when i use this function again after postback variable "done" is set to "false" and WriteFile function is called again althought I set it to true.

Is this because asp.net is stateless? Can I get around this using ViewState to store variable's value? If ViewState can be used, could someone write a
small example how should it be used? The help is very much appreciated.

Thanks in advance.

Peter

Nov 18 '05 #5
Thanks very much for your replies guys :) I think the ViewState is most
useful for my issue, so I try to use it.

Peter
"Peter" wrote:
Hello!

I Have a problem. I try to use asp.net page-wide variable but it is not
working. I declare my boolean variable (eg. bool done=false) in the same
place where page's web controls are declared. However when I try to use it in
a function, like

if (this.done==false) {
Response.Write("writing file")
this.WriteFile();
this.done=true;
}

But everytime when i use this function again after postback variable "done"
is set to "false" and WriteFile function is called again althought I set it
to true.

Is this because asp.net is stateless? Can I get around this using ViewState
to store variable's value? If ViewState can be used, could someone write a
small example how should it be used? The help is very much appreciated.

Thanks in advance.

Peter

Nov 18 '05 #6

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

Similar topics

1
by: Newbie | last post by:
OK, this may be impossible since I'm using 3rd party shopping cart ASP software, but I've been able to finagle a lot of other stuff I thought wouldn't work, so here we go: I'm using a form in...
1
by: Ann Leland | last post by:
I have been using session variables to pass a user name from one ASP page to another inside framesets for 9 months and it stopped working this week. I have made no code changes but there was a...
8
by: hb | last post by:
Hi, I need to declare a variable who's value can be preserve through the same ASP.Net page. I tried the following code, only the static variable s2 keeps its value=22 after lnk1_Click followed...
10
by: darrel | last post by:
I have this structure: mypage.aspx (class = mypage) myusercontro.ascx On the mypage.aspx I can declare a variable: animal = "monkey" I can read this from the UC by simply doing this:...
3
by: aaa | last post by:
I fail to see the connection between the code behind and the raw HTML of the ASPX page how do you get variables and functions to communicate with each other? I cannot get this to even fire:...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
9
by: KenLee | last post by:
I made an application which includes classic asp page and asp.net page. when I tried to redirect from classic asp page to asp.net 2.0 page, it works under my local IIS directory. ex) <a...
4
by: Sandeep Singh | last post by:
I m building an application in which I redirect the user to a particular page. In the Page Load event of the that page , I assign a variable some value. Then I call the Button click event on that...
7
by: Schmidty | last post by:
Okay...I have another 'newbie' question; I have a function that loads a page and the action is $_SERVER; In the form that is in a function(method?) within a class a variable is passed back to...
6
by: John Kotuby | last post by:
Hi all, I am using a 3rd party program in a VS2005 web project. The tool takes as input a string containing HTML and converts it to RTF. I have been creating a page by dynamically loading...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.