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