The ClientState Web Form Code-Behind Class File
using System;
namespace ControlsBook2Web.Ch03
{
public partial class ClientState : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GetClientState();
}
protected void SetStateButton_Click(object sender, EventArgs e)
{
SetClientState();
}
private void SetClientState()
{
string name = NameTextBox.Text;
// set the name Cookie value
Response.Cookies["cookiename"].Value = name;
// encode the name in the redirect URL
URLEncodeLink.NavigateUrl = "ClientState.aspx?urlname=" + name;
CHAPTER 3 ?– A SP.NET S TATE MANAGEMENT 99
// put the name in the hidden variable
HiddenName.Value = name;
// put the name in ViewState
ViewState["viewstatename"] = name;
}
private void GetClientState()
{
// check the cookiename Cookie
CookieLabel.Text = "";
if (Request.Cookies["cookiename"] != null)
CookieLabel.Text = Request.Cookies["cookiename"].Value;
// check the URL for urlname variable
URLLabel.Text = "";
if (Request.QueryString["urlname"] != null)
URLLabel.Text = Request.Params["urlname"];
// check the form data for hiddenname variable
// Must use UniqueID to get correct HTML Form name
HiddenLabel.Text = "";
if (Context.Request.Form[HiddenName.
Pages:
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183