The LabelControls Web Form Code-Behind Class File
using System;
namespace ControlsBook2Web.Ch03
{
public partial class LabelControls : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SetLabelButton_Click(object sender, EventArgs e)
{
StatelessLabel1.Text = "Set by " + NameTextBox.Text;
StatefulLabel1.Text = "Set by " + NameTextBox.Text;
}
}
}
Setting the Label Control State
Click the Set Labels button to post the web page back to the server. The SetLabelButton_Click
routine is executed by ASP.NET when the SetLabelButton button is notified that the HTML
button it represents caused the postback.
SetLabelButton_Click has the simple job of setting the Text property of the StatelessLabel
and StatefulLabel controls:
CHAPTER 3 ?– A SP.NET S TATE MANAGEMENT 107
protected void SetLabelButton_Click(object sender, EventArgs e)
{
StatelessLabel1.Text = "Set by " + NameTextBox.Text;
StatefulLabel1.Text = "Set by " + NameTextBox.Text;
}
The output of the web form for these controls is identical, because they both render their
contents into HTML based on the recently set Text property, as shown in Figure 3-9.
Figure 3-9. LabelControls.aspx after the Set Labels button postback
Testing Control ViewState
To test the state-saving features of ViewState and its impact on controls, click the Submit Page
button.
Pages:
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192