Variable Problems with C#

S

- Steve -

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.EventArgs e)
{
testString = "testing";
}

private void btnShow_Click(object sender, System.EventArgs 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)
 
G

Guest

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.EventArgs e

testString = "testing"


private void btnShow_Click(object sender, System.EventArgs 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
 
S

- Steve -

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.DirectoyServices.DirectoryEntry a lot. How do you work around that?

--

Steve Evans
Email Services
SDSU Foundation



Tu-Thach said:
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.
 
D

Dennis Homann

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.EventArgs e)
{
TestString = "testing";
}

private void btnShow_Click(object sender, System.EventArgs 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
 
G

Guest

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.DirectoyServices.DirectoryEntry a lot. How do you work around that

--

Steve Evan
Email Service
SDSU Foundatio



Tu-Thach said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top