Yeah... I wrote an example up before seeing Jimi's example.
But here is mine... Same bat time, same bat channel.
ASPX
<HTML>
<HEAD>
<title>ViewStateTest</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<P><asp:textbox id="TextBox1" runat="server"></asp:textbox> (Put a
Number In
Here, and click the Button Below)</P>
<P><asp:button id="Button1" runat="server"
Text="ButtonONE"></asp:button></P>
<P><asp:button id="Button2" runat="server" Text="ButtonTWO"
Visible="False"></asp:button></P>
</form>
</body>
</HTML>
..cs code behind:
public int SelectedRecordID
{
get
{
if (ViewState["SelectedRecordID"] != null)
return Convert.ToInt32(ViewState["SelectedRecordID"]);
else
return -1;
}
set
{
ViewState["SelectedRecordID"] = value;
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
int whatsMyValue = this.SelectedRecordID;
this.SelectedRecordID = int.Parse(this.TextBox1.Text);
this.Button1.Visible = false;
this.Button2.Visible = true;
}
private void Button2_Click(object sender, System.EventArgs e)
{
int whatsMyValue = this.SelectedRecordID;
this.SelectedRecordID = int.Parse(this.TextBox1.Text);
}
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!Page.IsPostBack)
{
this.SelectedRecordID = -999; // use -999 for the PageLoad
}
}
"Jimi200478" <JM*********@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
Adam Sandler:
You are going to have to store "MyName" in either ViewState or Session
if you want it to survive a PostBack. Both mine and Sloan's examples
are the same, just different syntax. Sloans example demonstrates how to
use a property in .NET, but it doesnt answer base question which is
"how do i make the value survive a postback". The answer to that
question is viewstate or session. if you do want to have a variable on
your page called myname, you will have to do something like this:
in C#:
private string _MyName;
public string MyName
{
get
{
_MyName = ViewState["MyName"].ToString();
return _MyName;
}
set
{
_MyName = value;
ViewState["MyName"] = _MyName;
}
}
if the property is followed by an equals sign, then it knows to "set"
the value. if it is called without an equals sign, the property knows
to "get" the value. see below:
Setting MyName :
MyName = "John"
Getting MyName :
MsgBox(MyName)
Truthfully, at the end of it all, it doesnt matter how you do it, all
that matters is that you use viewstate or session to keep your value
alive. well, good luck!