473,763 Members | 6,149 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Variable Problems with C#

I have a cs file I use in an ASP.NET form. The class has several member
variables. If I assign a value to those variables in a method, when I get
to another method the variable no longer equals what it was assigned.

For example:

namespace test
{
public class testClass : Page
{
protected String testString;

private void btnEmail_Click( object sender, System.EventArg s e)
{
testString = "testing";
}

private void btnShow_Click(o bject sender, System.EventArg s e)
{
//What will print here is "testString equals - " and that is it.
lblMessage.Text = "testString equals - " + testString;
}
}

in the btnShow_Click shouldn't testString equal "testing" from when it was
assigned in btnEmail_Click? Everything works fine with similiar code in a
console C# application. I'm really confused on what I'm screwing up. (ps
I'm new to ASP.NET)
--

Steve Evans
Email Services
SDSU Foundation


Nov 18 '05 #1
4 1399
That is the correct behavior of ASP.NET. If you want your variable to persist between requests, you can use ViewState, Session variables or hidden form field

Tu-Thac
www.ongtech.co

----- - Steve - wrote: ----

I have a cs file I use in an ASP.NET form. The class has several membe
variables. If I assign a value to those variables in a method, when I ge
to another method the variable no longer equals what it was assigned

For example

namespace tes

public class testClass : Pag

protected String testString

private void btnEmail_Click( object sender, System.EventArg s e

testString = "testing"
private void btnShow_Click(o bject sender, System.EventArg s e

//What will print here is "testString equals - " and that is it
lblMessage.Text = "testString equals - " + testString

in the btnShow_Click shouldn't testString equal "testing" from when it wa
assigned in btnEmail_Click? Everything works fine with similiar code in
console C# application. I'm really confused on what I'm screwing up. (p
I'm new to ASP.NET
--

Steve Evan
Email Service
SDSU Foundatio

Nov 18 '05 #2
I was looking at those three options - ViewState, Session Variables, and
hidden form fields, and it appears there's no way to declare any of those as
a certain variable type. For example I use
System.Directoy Services.Direct oryEntry a lot. How do you work around that?

--

Steve Evans
Email Services
SDSU Foundation

"Tu-Thach" <an*******@disc ussions.microso ft.com> wrote in message
news:83******** *************** ***********@mic rosoft.com...
That is the correct behavior of ASP.NET. If you want your variable to persist between requests, you can use ViewState, Session variables or hidden
form field.
Tu-Thach
www.ongtech.com

----- - Steve - wrote: -----

I have a cs file I use in an ASP.NET form. The class has several member variables. If I assign a value to those variables in a method, when I get to another method the variable no longer equals what it was assigned.

For example:

namespace test
{
public class testClass : Page
{
protected String testString;

private void btnEmail_Click( object sender, System.EventArg s e) {
testString = "testing";
}

private void btnShow_Click(o bject sender, System.EventArg s e)
{
//What will print here is "testString equals - " and that is it. lblMessage.Text = "testString equals - " + testString;
}
}

in the btnShow_Click shouldn't testString equal "testing" from when it was assigned in btnEmail_Click? Everything works fine with similiar code in a console C# application. I'm really confused on what I'm screwing up. (ps I'm new to ASP.NET)
--

Steve Evans
Email Services
SDSU Foundation


Nov 18 '05 #3
Steve,

in ASP.NET a new instance of your page is instantiated for each request,
that is each button click. You do not override OnInit to initialize
testString. Therefore, it will be null whenever btnEmail_Click or
btnShow_Click are executed.

The point here is that the only values which live longer than one request
are those saved in the ViewState object. Server controls typically store
enough information in the view state to restore their state. Try this:

namespace test
{
public class testClass : Page
{
protected string TestString {
get { return (string) ViewState["testString "]; }
set { ViewState["testString "] = value; }
}

private void btnEmail_Click( object sender, System.EventArg s e)
{
TestString = "testing";
}

private void btnShow_Click(o bject sender, System.EventArg s e)
{
//What will print here is "testString equals - " and that is
it.
lblMessage.Text = "testString equals - " + TestString;
}
}
}

Check out the tutorials at www.asp.net to learn more.

- Dennis
"- Steve -" <se****@foundat ion.sdsu.edu> schrieb im Newsbeitrag
news:O3******** ******@TK2MSFTN GP10.phx.gbl...
I have a cs file I use in an ASP.NET form. The class has several member
variables. If I assign a value to those variables in a method, when I get
to another method the variable no longer equals what it was assigned.

For example:

namespace test
{
public class testClass : Page
{
protected String testString;

private void btnEmail_Click( object sender, System.EventArg s e)
{
testString = "testing";
}

private void btnShow_Click(o bject sender, System.EventArg s e)
{
//What will print here is "testString equals - " and that is it. lblMessage.Text = "testString equals - " + testString;
}
}

in the btnShow_Click shouldn't testString equal "testing" from when it was
assigned in btnEmail_Click? Everything works fine with similiar code in a
console C# application. I'm really confused on what I'm screwing up. (ps
I'm new to ASP.NET)
--

Steve Evans
Email Services
SDSU Foundation

Nov 18 '05 #4
You can store the DirectoryEntry in session variables

Tu-Thac

----- - Steve - wrote: ----

I was looking at those three options - ViewState, Session Variables, an
hidden form fields, and it appears there's no way to declare any of those a
a certain variable type. For example I us
System.Directoy Services.Direct oryEntry a lot. How do you work around that

--

Steve Evan
Email Service
SDSU Foundatio

"Tu-Thach" <an*******@disc ussions.microso ft.com> wrote in messag
news:83******** *************** ***********@mic rosoft.com..
That is the correct behavior of ASP.NET. If you want your variable t persist between requests, you can use ViewState, Session variables or hidde
form field
Tu-Thac

www.ongtech.co
----- - Steve - wrote: ----
I have a cs file I use in an ASP.NET form. The class has severa membe variables. If I assign a value to those variables in a method, whe I ge to another method the variable no longer equals what it was assigned
For example
namespace tes
public class testClass : Pag

protected String testString
private void btnEmail_Click( object sender, System.EventArg

e
testString = "testing"
private void btnShow_Click(o bject sender, System.EventArg s e
//What will print here is "testString equals - " and tha

is it lblMessage.Text = "testString equals - " + testString

in the btnShow_Click shouldn't testString equal "testing" from whe
it wa assigned in btnEmail_Click? Everything works fine with similiar cod in console C# application. I'm really confused on what I'm screwing up (p I'm new to ASP.NET
--
Steve Evan

Email Service
SDSU Foundatio>

Nov 18 '05 #5

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

Similar topics

0
1462
by: Philip Rittenhouse | last post by:
I have discovered a couple of problems with the way the universal gateway code handles optional parameters and variable argument lists in COM servers. It appears to only be a problem when you use the custom interface. What I found was that variable argument lists are not detected at all. Instead they are just converted from SAFEARRAYs to a Python list.
83
6522
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in C. It can cause very ugly errors,like this: epsilon=0 S=0 while epsilon<10: S=S+epsilon
12
5881
by: David WOO | last post by:
Hi, I am a newbie on C++, I need to define some global variables which should be accessible to most classes. In the mean time, I don't won't the global variables be modified freely at most of these classes. I know there is a pattern called singleton can more or less do such a trick. I am wondering is this the best way to do it (regarding the convenience and safety), as this is such a fundamental thing, I believe most of you have a say...
10
429
by: Peter Kirk | last post by:
Hi there I have a question about static variables. There is a function which returns a pointer to a structure "AStructure". The function has a static local variable of type AStructure, and it is the address of this variable which is returned. Is this good practice? See this pseudo code: AStructure *my_function()
19
2147
by: Skybuck Flying | last post by:
Hi, I think I might have just invented the variable bit cpu :) It works simply like this: Each "data bit" has a "meta data bit". The meta data bit describes if the bit is the ending bit of a possibly large structure/field.
24
2529
by: LP | last post by:
After a code review one coworker insisted that global are very dangerous. He didn't really give any solid reasons other than, "performance penalties", "hard to maintain", and "dangerous". I think that I am using them appropriate in class in question. One typical example: This class initiates TCP session, keeps sending commands to the server app and gets messages and codes back, message and code are stored in global variables to be preserved...
5
4135
by: Marc Rivait | last post by:
Here is a very interesting scenario. I have a simple test application that loads a page and sets a session variable on the load event. On the first page there is a link to a second page. The load event of the second page displays the value of the session variable. The problem is that when I use a W2k machine, this little test works perfect. Session variable value is displayed on the second page. However, when I use my XPPro...
24
1615
by: Desmond Cassidy | last post by:
Hi, I don't know whether this is the correct place to ask this question..anyway here goes... I have a large solution with about 300 .vb files split into 9 projects VB.net Framework 1.1 WinXP Pro Whilst editing, as soon as I try and enter a statement like Private strName as string
1
3540
by: Vidyadhar Joshi | last post by:
I have the following scenario in a true load balanced environment (without sticky sessions): There are 2 ASPX pages. I want to pass an object from the first page to the second page. On the btnContinue_Click event of Page1.aspx, I create the object and store it in a session variable. The next statement would be Response.Redirect("Page2.aspx"). The code appears like this: private void btnContinue_Click(object sender, EventArgs e) {...
1
11865
NeoPa
by: NeoPa | last post by:
Problem Description : In VBA there is an option to enforce declaration of variables in your code. With this set, any reference to a variable that has not been previously declared (Dim; Private; Public; Global; etc) will cause an error, either at compile time or when attempting to execute the procedure it's referred to from within. With this unset, any unregognised references will be treated as a previously unknown and unset variable of type...
0
9563
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10144
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9997
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9937
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.