15.5 Responding to Postback Events

The <asp:button> objects automatically postback when clicked. You need not write any code to handle that event unless you want to do something more than postback to the server. If you take no other action, the page will simply be resent to the client.

Normally, when a page is redrawn, each control is redrawn from scratch. The Web is stateless, and if you want to manage the state of a control (e.g., redraw the user's text in the text box), you must do so yourself. In classic ASP, the programmer was responsible for managing this state, but ASP.NET provides some assistance. When the page is posted, a hidden element named ViewState is automatically added to the page:

<input type="hidden" name="_  _VIEWSTATE"  value="YTB6LTI5MTE3ODE1N19hMHpfaHo1ejF4X2Ewel9oejV6NXhfYTB6YTB6YTB6aHpSZXBlYX RMYXlvdXRfU3lzdGVtLldlYi5VSS5XZWJDb250cm9scy5SZXBlYXRMYXlvdXR6VGFibGV4X0RhdGF WYWx1ZUZpZWxkX1NoaXBwZXJJRF9EYXRhVGVceHRGaWVsZF9Db21wYW55TmFtZXhfX3hfYTB6YTB6 YXpTcGVlZHkgRVx4cHJlc3NfMV94X2F6VW5pdGVkIFBhY2thZ2VfMl94X2F6RmVkZXJhbCBTaGlwc GluZ18zX3hfeF94X3hfX3h4X3h4X3hfX3hcdDUwX1N5c3RlbS5TdHJpbmc=a15204ed" />

This element represents the state of the form (the values are already chosen by the user). When the page is redrawn on the client, ASP.NET uses the view state to return the controls to their previous state.

When the user clicks the Order button, the page is posted and the event handler assigned to that button is invoked:

public void Order_Click (object sender, System.EventArgs e) {     string msg;     msg = "Thank you " + txtName.Text +". You chose " ;     for (int i = 0;i<rbl1.Items.Count;i++)     {         if (rbl1.Items[i].Selected)         {             msg = msg + rbl1.Items[i].Text;             lblFeedBack.Text = msg;         }     } }

The easiest way to create the event handler is to double-click the Order button in Design mode in Visual Studio .NET. This will cause Visual Studio to add the event to the InitializeComponent method:

Order.Click += new System.EventHandler (this.Order_Click);

It will also create a skeleton Order_Click event-handler method for you. Alternatively, you can do this all by hand.

This event handler creates a message based on the name you enter and the shipper you choose, and puts that message into the Feedback label. When the form first comes up, it looks like Figure 15-5. If I fill in my name, pick United Package, and press Order, the form will be submitted and then redisplayed. The result is shown in Figure 15-6.

Figure 15-6. Page posted after the user clicks Order
figs/pcsharp3_1506.gif

The form automatically remembers the state of the radio button and text controls (this is what the ViewState field is for) and that the event handler has been called and run on the server; the label is updated accordingly.

ASP programmers take note: There is no code in the .aspx file nor in the .cs file to manage the state. Nowhere do you stash away the state of the radio buttons or the text field; all this is managed automatically for you by ASP.NET.



Programming C#
C# Programming: From Problem Analysis to Program Design
ISBN: 1423901460
EAN: 2147483647
Year: 2003
Pages: 182
Authors: Barbara Doyle

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net