Handling PostBack s

I l @ ve RuBoard

Handling PostBack s

All custom controls can take advantage of the built-in ViewState property of the Control component to handle retaining their value through PostBack s. The use of ViewState can be controlled through the EnableViewState property of a control, or at the Page level with the EnableViewState property of the Page . When used, ViewState is stored in an encrypted hidden form field called __VIEWSTATE , which you can examine by viewing the source of an ASP.NET page in your browser ( assuming it is using ViewState ).

Let's update our HelloWorld control to accept a property, and use ViewState to persist this property across multiple PostBack s. We'd like to be able to say hello to someone in particular, so we'll add a PersonName property to the control, and then have it render "Hello" + PersonName instead of "Hello World". In order to utilize ViewState , we'll reference the ViewState collection in our set and get methods of the property. The complete source for this updated control is found in Listing 12.9.

Listing 12.9 HelloPerson.cs
 namespace ASPNETByExample {     public class HelloPerson : System.Web.UI.Control     {         public string PersonName         {             get             {                 return ViewState["PersonName"].ToString();             }             set             {                 ViewState["PersonName"] = value;             }         }         protected override void Render(System.Web.UI.HtmlTextWriter htwOutput)         {             htwOutput.Write("Hello " + PersonName);         }     } } 

As you can see, this is not very different from the other controls we have built thus far, except for the get and set methods of our PersonName property, which use the ViewState collection of the Control object to store state information for this control. Note that the collection is specific to this control, so if we were to put more than one HelloPerson control on a page, and set each one's PersonName property to something different, we would not have any collisions; everything would work fine.

To test this control, we can build a simple ASP.NET page like the one shown in Listing 12.10.

Listing 12.10 HelloPerson.aspx, C#
 <%@ Page Language="C#" %> <%@ Register TagPrefix="Hello" Namespace="ASPNETByExample" Assembly="HelloWorld" %> <%@ Import Namespace="ASPNETByExample" %> <script runat="server"> void Page_Load(){     if(!IsPostBack){         hello.PersonName = "Steve";     } } </script> <html>     <body>         <form runat="server">             <Hello:HelloPerson runat="server" id="hello" />             <br />             <asp:Button Runat="server" Text="PostBack" />         </form>     </body> </html> 

In the Page_Load event handler for this page, we check to see if the current instance of the page is due to a PostBack by using the Page.IsPostBack property. If it is not a PostBack , such as when the page is first loaded, we set the PersonName property of our HelloPerson control (named hello in the code) to Steve . Note that, in the event of a PostBack , we do not set any value.

Clicking on the button will result in the control retaining its original value of "Hello Steve". If we add Trace="true" to the Page directive of the page, we will be able to see even more information, such as the size of the ViewState for this control (36 bytes), the __VIEWSTATE contents, and the start and end of the PostBackEvent event in the Trace Information.

You can perform more advanced operations related to ViewState and PostBack s, such as comparing the value of the posted data with the current value of the control to see if it has changed, or responding to a PostBack event by raising an event of its own. These advanced techniques are beyond the scope of this book to cover, but are accomplished by implementing interfaces such as the IPostBackDataHandler and IPostBackEventHandler . Searching for these in the .NET documentation or online should help you get started with building controls that use these features.

I l @ ve RuBoard


Asp. Net. By Example
ASP.NET by Example
ISBN: 0789725622
EAN: 2147483647
Year: 2001
Pages: 154

Similar book on Amazon

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