Recipe 6.4. Creating a Custom Control with State


Problem

You want to create a custom control that remembers its state between postbacks of a form, like the server controls provided with ASP.NET.

Solution

Create a custom control like the one described in Recipe 6.2, implement the IPostBackDataHandler interface to add the functionality to retrieve the values posted to the server and then update the values in the custom control from the postback data.

Use the .NET language of your choice to:

  1. Create a class that inherits from the WebControl class in the System.Web.UI.WebControls namespace.

  2. Implement support for HTML-style attributes by adding properties to the class.

  3. Implement an IPostBackDataHandler as necessary to update the state of the control with the posted data.

  4. Override the RenderContents method to have it render the HTML output of the control using the values of the properties.

To use the custom control in an ASP.NET page:

  1. Register the assembly containing the control.

  2. Insert the tag for the custom control anywhere in the page and set the attributes appropriately.

Examples 6-7 and 6-8 show the VB and C# class files for a custom control that maintains state. Example 6-9 shows how we use the custom control in an ASP.NET page.

A version of the custom control that maintains state and provides the added ability to raise an event when the control data has changed is shown in Examples 6-10 (VB) and 6-11 (C#). Examples 6-12, 6-13 through 6-14 show the .aspx and code-behind files of an application that uses the custom control with state and provides an event handler for the data-changed event.

Discussion

When implementing a custom control, you will want to ensure it maintains its state between postbacks to the server. Otherwise, it will lose its values and the user will have to reset them each time. To see what we mean by this, consider the custom controls discussed in Recipes 6.1 and 6.2; if you implement either of these controls, you will notice that anytime you click the Submit button, the value entered into the text box is lost when the page is redisplayed. This is caused by the control not processing the form data posted back to the server.

To maintain the values in a custom control, the control must implement the IPostBackDataHandler interface. The IPostBackDataHandler interface requires the implementation of two methods: LoadPostData and RaisePostDataChangedEvent. The LoadPostData method supplies the data posted to the server, which provides the ability to update the state of the control with the posted data. The RaisePostDataChangedEvent method provides the ability to raise events if data for the control changes; this method is used to good effect in the second of the two approaches we advocate for this recipe, and we discuss it at the end of this section. The LoadPostData and RaisePostDataChangedEvent methods are automatically called by ASP.NET when the form is posted back to the server.

The LoadPostData method has the following signature:

 

Public Overridable Function LoadPostData(ByVal postDataKey As String, _ ByVal postCollection As NameValueCollection) As Boolean

public virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)

The postCollection argument contains the collection of name/value pairs posted to the server. The postDataKey parameter provides the "name" of the key value for this control used to access the control's postback value.

The postDataKey parameter will be set to the unique ID value for the custom control. If the custom control contains only one control that posts a value back to the server and its ID was used as the value of the name attribute when the control was rendered, the value of the postDataKey parameter can be used to obtain the posted value. If the custom control is a composite control that contains more than one control with postback data or if the custom control's ID is not used for the control within the custom control, you, as the programmer, will need to provide a unique ID value for each control in the custom control, extract the values individually within the LoadPostData method, and set the appropriate property.

The return value for the LoadPostData method should always be set to False if the data has not changed or if no check is performed to see if the data changed. Setting the return to False prevents the RaisePostDataChangedEvent method from being called. See the discussion at the end of this section regarding raising events on data changes.


To give you a better feel for how to implement this solution, we turn to a variation of the custom control we've been working with throughout the chapter, which contains a label and text box. In our first example, the control needs to remember the state of the text in the text box of the input HTML control, and so we have added a text property to provide the ability to get/set the text value. Similar properties are provided for the label text, text color, and text box width. No surprise here.

As you look over the code, you'll notice that, with the exception of the text property, all the properties in this custom control use private variables to store their values. These values are lost when the form is rendered and sent to the browser. Because these values are not changed by the user in the browser, there is no need to remember their previous values. The text property is another matter. To provide the ability to remember its previous value, we use the ViewState to persist the value instead of a private variable. We have implemented the LoadPostData method to process the data posted back from the client and set the text property from the postback data.

We have also implemented the RaisePostDataChangedEvent method. In this first example, the method is empty and not used but is required as part of the IPostBackDataHandler interface. In the recipe's second example, it is used to raise an event when data for the text control change (more about this later).

A couple of changes from Recipe 6.2's implementation are required in the RenderContents event handler. You must set the name and value attributes of the HTML input control. Set the name attribute to a unique identifier so you'll have the ability to obtain the value posted back to the server. If the control does not have a name attribute, the browser will not include its data in the postback data. Set the value attribute to the current text value for the control.

If the text value does not exist, the value attribute should not be output. Absent this check, the value attribute will be output without a value, which is not well-formed HTML and is not handled well by some browsers.


In our second example, we build on the basic structure of the first solely to raise an event if the text in the text box changes and to notify the user with a change in the label text. Because the value that was output when the page was rendered is stored in the ViewState, we can compare it to the new value posted back to the server. As shown in Examples 6-10 (VB) and 6-11 (C#), if the value changes, the LoadPostData method will return TRue. Otherwise, it will return false.

When the LoadPostData method returns True, ASP.NET will call the RaisePostDataChangedEvent method after all of the controls have had the opportunity to process their postback data. In this method, we raise the TextChanged event, passing a reference to the custom control and any event arguments that are applicable, as shown in Examples 6-10 (VB) and 6-11 (C#). In this example, no arguments are required, so EventArgs.Empty is used for the event arguments parameter.

See Also

Recipes 6.1, 6.2, and 6.4

Example 6-7. Custom control with state (.vb)

 Option Explicit On Option Strict On Imports System.Drawing Namespace ASPNetCookbook.VBExamples ''' <summary> ''' This class provides a custom control that maintains state through ''' postbacks to the server. ''' </summary> Public Class CH06CustomControlWithStateVB1 Inherits WebControl Implements IPostBackDataHandler 'private copies of attribute data Private mLabelText As String = "Label: " Private mTextColor As Color = Color.Black Private mTextboxWidth As Integer = 3 '''*********************************************************************** ''' <summary> ''' This property provides the ability to set the text of of the label ''' in the control ''' </summary> Public Property labelText() As String Get Return (mLabelText) End Get Set(ByVal Value As String) mLabelText = Value End Set End Property 'labelText '''*********************************************************************** ''' <summary> ''' This property provides the ability to set the color of the text ''' in the control ''' </summary> Public Property textColor() As Color Get Return (mTextColor) End Get Set(ByVal Value As Color) mTextColor = Value End Set End Property 'textColor '''*********************************************************************** ''' <summary> ''' This property provides the ability to set the width of the text box ''' in the control ''' </summary> Public Property textboxWidth() As Integer Get Return (mTextboxWidth) End Get Set(ByVal Value As Integer) mTextboxWidth = Value End Set End Property 'textboxWidth '''*********************************************************************** ''' <summary> ''' This property provides the ability to set the HTML tag that is used ''' as the container for the control ''' </summary> Protected Overrides ReadOnly Property TagKey() As HtmlTextWriterTag Get Return (HtmlTextWriterTag.Div) End Get End Property 'TagKey '''*********************************************************************** ''' <summary> ''' This property provides the ability to set the text in the text box ''' in the control. NOTE: The text value is stored in the ViewState ''' instead of a private variable to provide the ability to check the ''' current and previous values to determine if the text has changed. ''' </summary> Private Const VS_TEXTBOX_VALUE As String = "TextboxValue" Public Property text() As String Get Dim value As String = Nothing If (Not IsNothing(viewstate(VS_TEXTBOX_VALUE))) Then value = CStr(viewstate(VS_TEXTBOX_VALUE)) End If Return (value) End Get Set(ByVal Value As String) ViewState(VS_TEXTBOX_VALUE) = Value End Set End Property 'text '''*********************************************************************** ''' <summary> ''' This routine renders the HTML output of the control contents ''' </summary> ''' ''' <param name="writer">Set to the HtmlTextWriter to use to output the ''' rendered HTML for the control ''' </param> Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter) 'output label within a font tag writer.AddAttribute("color", _ ColorTranslator.ToHtml(textColor)) writer.RenderBeginTag(HtmlTextWriterTag.Font) writer.Write(labelText) writer.RenderEndTag() 'output input control writer.AddAttribute(HtmlTextWriterAttribute.Type, _ "text") writer.AddAttribute(HtmlTextWriterAttribute.Size, _ textboxWidth.ToString()) 'output name attribute to identify data on postback writer.AddAttribute(HtmlTextWriterAttribute.Name, _ Me.UniqueID) 'output value attribute only if value exists If (Not IsNothing(text)) Then writer.AddAttribute(HtmlTextWriterAttribute.Value, _ text) End If writer.RenderBeginTag(HtmlTextWriterTag.Input) writer.RenderEndTag() End Sub 'RenderContents '''*********************************************************************** ''' <summary> ''' This routine processes data posted back from the client ''' </summary> ''' ''' <param name="postDataKey">The key identifier for the control</param> ''' <param name="postCollection">The collection of all incoming name ''' values</param> ''' ''' <returns>set true if the server control's state changes as a result ''' of the post back; otherwise false ''' </returns> Public Overridable Function LoadPostData(ByVal postDataKey As String, _ ByVal postCollection As NameValueCollection) As Boolean _ Implements IPostBackDataHandler.LoadPostData 'set the value of the text property from the postback data text = postCollection(postDataKey) Return (False) End Function 'LoadPostData '''*********************************************************************** ''' <summary> ''' This routine processes data changed events as a result of the postback ''' </summary> Public Overridable Sub RaisePostDataChangedEvent() _ Implements IPostBackDataHandler.RaisePostDataChangedEvent End Sub 'RaisePostDataChangedEvent   End Class End Namespace 

Example 6-8. Custom control with state (.cs)

 using System; using System.Collections.Specialized; using System.Drawing; using System.Web.UI; using System.Web.UI.WebControls; namespace ASPNetCookbook.CSExamples { /// <summary> /// This class provides a custom control that maintains state through /// postbacks to the server. /// </summary> public class CH06CustomControlWithStateCS1 : WebControl, IPostBackDataHandler { // private copies of attribute data private String mLabelText = "Label: "; private Color mTextColor = Color.Black; private int mTextboxWidth = 3; ///********************************************************************** /// <summary> /// This property provides the ability to get/set the text of of the label /// in the control /// </summary> public String labelText { get { return (mLabelText); } set { mLabelText = value; } } // labelText ///*********************************************************************** /// <summary> /// This property provides the ability to set the color of the text /// in the control /// </summary> public Color textColor { get { return (mTextColor); } set { mTextColor = value; } } // textColor ///*********************************************************************** /// <summary> /// This property provides the ability to get/set the width of the text box /// in the control /// </summary> public int textboxWidth { get { return (mTextboxWidth); } set { mTextboxWidth = value; } } // textboxWidth ///*********************************************************************** /// <summary> /// This property provides the ability to set the HTML tag that is used /// as the container for the control /// </summary> protected override HtmlTextWriterTag TagKey { get { return (HtmlTextWriterTag.Div); } } // TagKey ///*********************************************************************** /// <summary> /// This property provides the ability to get/set the text in the text box /// in the control /// </summary> private const String VS_TEXTBOX_VALUE = "TextboxValue"; public String text { get { String value = null; if (ViewState[VS_TEXTBOX_VALUE] != null) { value = (String)(ViewState[VS_TEXTBOX_VALUE]); } return (value); } set { ViewState[VS_TEXTBOX_VALUE] = value; } } // text ///*********************************************************************** /// <summary> /// This routine renders the HTML output of the control contents /// </summary> /// /// <param name="writer">Set to the HtmlTextWriter to use to output the /// rendered HTML for the control /// </param> protected override void RenderContents(HtmlTextWriter writer) { //output label writer.AddAttribute("color", ColorTranslator.ToHtml(textColor)); writer.RenderBeginTag(HtmlTextWriterTag.Font); writer.Write(labelText); writer.RenderEndTag(); //output input control writer.AddAttribute(HtmlTextWriterAttribute.Type, "text"); writer.AddAttribute(HtmlTextWriterAttribute.Size, textboxWidth.ToString()); // output name attribute to identify data on postback writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID); // output value attribute only if value exists if (text != null) { writer.AddAttribute(HtmlTextWriterAttribute.Value, text); } writer.RenderBeginTag(HtmlTextWriterTag.Input); writer.RenderEndTag(); } // RenderContents ///*********************************************************************** /// <summary> /// This routine processes data posted back from the client /// </summary> /// /// <param name="postDataKey">The key identifier for the control</param> /// <param name="postCollection">The collection of all incoming name /// values</param> /// /// <returns>set true if the server control's state changes as a result /// of the post back; otherwise false /// </returns> public virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection) { // set the value of the text property from the postback data text = postCollection[postDataKey]; return (false); } // LoadPostData ///*********************************************************************** /// <summary> /// This routine processes data changed events as a result of the postback /// </summary> public virtual void RaisePostDataChangedEvent() { } // RaisePostDataChangedEvent } // CH06CustomControlWithStateCS1 } 

Example 6-9. Using the custom control with state (.aspx)

 <%@ Page Language="VB" MasterPageFile="~/ASPNetCookbookVB.master" AutoEventWireup="false" CodeFile="CH06DisplayControlWithStateVB1.aspx.vb" Inherits="ASPNetCookbook.VBExamples.CH06DisplayControlWithStateVB1" Title="Custom Control With State" %> <%@ Register TagPrefix="ASPCookbook" Namespace="ASPNetCookbook.VBExamples" %> <asp:Content  runat="server" ContentPlaceHolder> <div align="center" > Custom Control With State (VB) </div> <table width="90%" align="center" border="0"> <tr bgcolor="#ffffcc"> <td align="center"> <ASPCookbook:CH06CustomControlWithStateVB1  runat="server" labelText="Enter Age: " textColor="#000080" textboxWidth="5" /> </td> </tr> <tr> <td align="center"> <br/> <asp:Button  runat="server"    Text="Submit" /> </td> </tr> </table> </asp:Content> 

Example 6-10. Custom control with state and changed event (.vb)

 Option Explicit On Option Strict On Imports System.Drawing Namespace ASPNetCookbook.VBExamples ''' <summary> ''' This class provides a custom control that maintains state through ''' postbacks to the server and raises an event when the entered ''' data changes. ''' </summary> Public Class CH06CustomControlWithStateVB2 Inherits WebControl Implements IPostBackDataHandler 'define an event to be raised if the text changes Public Event TextChanged As EventHandler 'private copies of attribute data Private mLabelText As String = "Label: " Private mTextColor As Color = Color.Black Private mTextboxWidth As Integer = 3 '''*********************************************************************** ''' <summary> ''' This property provides the ability to set the text of of the label ''' in the control ''' </summary> Public Property labelText() As String Get Return (mLabelText) End Get Set(ByVal Value As String) mLabelText = Value End Set End Property 'labelText '''*********************************************************************** ''' <summary> ''' This property provides the ability to set the color of the text ''' in the control ''' </summary> Public Property textColor() As Color Get Return (mTextColor) End Get Set(ByVal Value As Color) mTextColor = Value End Set End Property 'textColor '''*********************************************************************** ''' <summary> ''' This property provides the ability to set the width of the text box ''' in the control ''' </summary> Public Property textboxWidth() As Integer Get Return (mTextboxWidth) End Get Set(ByVal Value As Integer) mTextboxWidth = Value End Set End Property 'textboxWidth '''*********************************************************************** ''' <summary> ''' This property provides the ability to set the HTML tag that is used ''' as the container for the control ''' </summary> Protected Overrides ReadOnly Property TagKey() As HtmlTextWriterTag Get Return (HtmlTextWriterTag.Div) End Get End Property 'TagKey '''*********************************************************************** ''' <summary> ''' This property provides the ability to set the text in the text box ''' in the control. NOTE: The text value is stored in the ViewState ''' instead of a private variable to provide the ability to check the ''' current and previous values to determine if the text has changed. ''' </summary> Private Const VS_TEXTBOX_VALUE As String = "TextboxValue" Public Property text() As String Get Dim value As String = Nothing If (Not IsNothing(viewstate(VS_TEXTBOX_VALUE))) Then value = CStr(viewstate(VS_TEXTBOX_VALUE)) End If Return (value) End Get Set(ByVal Value As String) ViewState(VS_TEXTBOX_VALUE) = Value End Set End Property 'text '''*********************************************************************** ''' <summary> ''' This routine renders the HTML output of the control contents ''' </summary> ''' ''' <param name="writer">Set to the HtmlTextWriter to use to output the ''' rendered HTML for the control ''' </param> Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter) 'output label within a font tag writer.AddAttribute("color", _ ColorTranslator.ToHtml(textColor)) writer.RenderBeginTag(HtmlTextWriterTag.Font) writer.Write(labelText) writer.RenderEndTag() 'output input control writer.AddAttribute(HtmlTextWriterAttribute.Type, _ "text") writer.AddAttribute(HtmlTextWriterAttribute.Size, _ textboxWidth.ToString()) 'output name attribute to identify data on postback writer.AddAttribute(HtmlTextWriterAttribute.Name, _ Me.UniqueID) 'output value attribute only if value exists If (Not IsNothing(text)) Then writer.AddAttribute(HtmlTextWriterAttribute.Value, _ text) End If writer.RenderBeginTag(HtmlTextWriterTag.Input) writer.RenderEndTag() End Sub 'RenderContents '''*********************************************************************** ''' <summary> ''' This routine processes data posted back from the client ''' </summary> ''' ''' <param name="postDataKey">The key identifier for the control</param> ''' <param name="postCollection">The collection of all incoming name ''' values</param> ''' ''' <returns>set true if the server control's state changes as a result ''' of the post back; otherwise false ''' </returns> Public Overridable Function LoadPostData(ByVal postDataKey As String,_ ByVal postCollection As NameValueCollection) As Boolean _ Implements IPostBackDataHandler.LoadPostData Dim dataChanged As Boolean = False Dim postbackValue As String 'check to see if the data changed postbackValue = postCollection(postDataKey) If (Not postbackValue.Equals(text)) Then dataChanged = True End If 'set the value of the text property from the postback data text = postbackValue Return (dataChanged) End Function 'LoadPostData '''*********************************************************************** ''' <summary> ''' This routine processes data changed events as a result of the postback ''' </summary> Public Overridable Sub RaisePostDataChangedEvent() _ Implements IPostBackDataHandler.RaisePostDataChangedEvent RaiseEvent TextChanged(Me, EventArgs.Empty) End Sub 'RaisePostDataChangedEvent End Class 'CH06CustomControlWithStateVB2 End Namespace 

Example 6-11. Custom control with state and changed event (.cs)

 using System; using System.Collections.Specialized; using System.Drawing; using System.Web.UI; using System.Web.UI.WebControls; namespace ASPNetCookbook.CSExamples { /// <summary> /// This class provides a custom control that maintains state through /// postbacks to the server and raises an event when the entered /// data changes. /// </summary> public class CH06CustomControlWithStateCS2 : WebControl, IPostBackDataHandler { // define an event to be raised if the text changes public event EventHandler TextChanged; // private copies of attribute data private String mLabelText = "Label: "; private Color mTextColor = Color.Black; private int mTextboxWidth = 3; ///********************************************************************** /// <summary> /// This property provides the ability to get/set the text of of the label /// in the control /// </summary> public String labelText { get { return (mLabelText); } set { mLabelText = value; } } // labelText ///*********************************************************************** /// <summary> /// This property provides the ability to set the color of the text /// in the control /// </summary> public Color textColor { get { return (mTextColor); } set { mTextColor = value; } } // textColor ///*********************************************************************** /// <summary> /// This property provides the ability to get/set the width of the text box /// in the control /// </summary> public int textboxWidth { get { return (mTextboxWidth); } set { mTextboxWidth = value; } } // textboxWidth ///*********************************************************************** /// <summary> /// This property provides the ability to set the HTML tag that is used /// as the container for the control /// </summary> protected override HtmlTextWriterTag TagKey { get { return (HtmlTextWriterTag.Div); } } // TagKey ///*********************************************************************** /// <summary> /// This property provides the ability to get/set the text in the text box /// in the control /// </summary> private const String VS_TEXTBOX_VALUE = "TextboxValue"; public String text { get { String value = null; if (ViewState[VS_TEXTBOX_VALUE] != null) { value = (String)(ViewState[VS_TEXTBOX_VALUE]); } return (value); } set { ViewState[VS_TEXTBOX_VALUE] = value; } } // text ///*********************************************************************** /// <summary> /// This routine renders the HTML output of the control contents /// </summary> /// /// <param name="writer">Set to the HtmlTextWriter to use to output the /// rendered HTML for the control /// </param> protected override void RenderContents(HtmlTextWriter writer) { //output label writer.AddAttribute("color", ColorTranslator.ToHtml(textColor)); writer.RenderBeginTag(HtmlTextWriterTag.Font); writer.Write(labelText); writer.RenderEndTag(); //output input control writer.AddAttribute(HtmlTextWriterAttribute.Type, "text"); writer.AddAttribute(HtmlTextWriterAttribute.Size, textboxWidth.ToString()); // output name attribute to identify data on postback writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID); // output value attribute only if value exists if (text != null) { writer.AddAttribute(HtmlTextWriterAttribute.Value, text); } writer.RenderBeginTag(HtmlTextWriterTag.Input); writer.RenderEndTag(); } // RenderContents ///*********************************************************************** /// <summary> /// This routine processes data posted back from the client /// </summary> /// /// <param name="postDataKey">The key identifier for the control</param> /// <param name="postCollection">The collection of all incoming name /// values</param> /// /// <returns>set true if the server control's state changes as a result /// of the post back; otherwise false /// </returns> public virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection) { Boolean dataChanged = false; String postbackValue; // check to see if the data changed postbackValue = postCollection[postDataKey]; if (!postbackValue.Equals(text)) { dataChanged = true; } // set the value of the text property from the postback data text = postbackValue; return (dataChanged); } // LoadPostData ///*********************************************************************** /// <summary> /// This routine processes data changed events as a result of the postback /// </summary> public virtual void RaisePostDataChangedEvent() { // raise event if a handler is assigned if (TextChanged != null) { TextChanged(this, EventArgs.Empty); } } // RaisePostDataChangedEvent } // CH06CustomControlWithStateCS2 } 

Example 6-12. Using the custom control with state and changed event (.aspx)

 <%@ Page Language="VB" MasterPageFile="~/ASPNetCookbookVB.master" AutoEventWireup="false" CodeFile="CH06DisplayControlWithStateVB2.aspx.vb" Inherits="ASPNetCookbook.VBExamples.CH06DisplayControlWithStateVB2" Title="Custom Control With State" %> <%@ Register TagPrefix="ASPCookbook" Namespace="ASPNetCookbook.VBExamples" %> <asp:Content  runat="server" ContentPlaceHolder> <div align="center" > Custom Control With State And Events (VB) </div> <table width="90%" align="center" border="0"> <tr bgcolor="#ffffcc"> <td align="center"> <ASPCookbook:CH06CustomControlWithStateVB2  runat="server" labelText="Enter Age: " textColor="#000080" textboxWidth="5" OnTextChanged="ccAttributes_TextChanged" /> </td> </tr> <tr> <td align="center"> <asp:Label  Runat="server" /> </td> </tr> <tr> <td align="center"> <br/> <asp:Button  runat="server"        Text="Submit" /> </td> </tr> </table> </asp:Content> 

Example 6-13. Using the custom control with state and changed event code-behind (.vb)

 Option Explicit On Option Strict On Namespace ASPNetCookbook.VBExamples   ''' <summary>   ''' This class provides the code beside for   ''' CH06DisplayControlWithStateVB2.aspx   ''' </summary>   Partial Class CH06DisplayControlWithStateVB2     Inherits System.Web.UI.Page '''*********************************************************************** ''' <summary> ''' This routine provides the event handler for the page load event. It ''' is responsible for initializing the controls on the page. ''' </summary> ''' ''' <param name="sender">Set to the sender of the event</param> ''' <param name="e">Set to the event arguments</param> Private Sub Page_Load(ByVal sender As Object, _   ByVal e As System.EventArgs) Handles Me.Load   labMessage.Text = "" End Sub 'Page_Load '''*********************************************************************** ''' <summary> ''' This routine provides the event handler for the custom control text ''' changed event. ''' </summary> ''' ''' <param name="sender">Set to the sender of the event</param> ''' <param name="e">Set to the event arguments</param> Protected Sub ccAttributes_TextChanged(ByVal sender As Object, _   ByVal e As System.EventArgs) labMessage.Text = "Data Changed"  End Sub 'ccAttributes_TextChanged   End Class 'CH06DisplayControlWithStateVB2 End Namespace 

Example 6-14. Using the custom control with state and changed event code-behind (.cs)

 using System; namespace ASPNetCookbook.CSExamples {   /// <summary>   /// This class provides the code beside for   /// CH06DisplayControlWithStateCS2.aspx   /// </summary>   public partial class CH06DisplayControlWithStateCS2 : System.Web.UI.Page   {     ///*********************************************************************** /// <summary> /// This routine provides the event handler for the page load event. /// It is responsible for initializing the controls on the page. /// </summary> /// /// <param name="sender">Set to the sender of the event</param> /// <param name="e">Set to the event arguments</param> protected void Page_Load(object sender, EventArgs e) {   labMessage.Text = ""; } // Page_Load ///*********************************************************************** /// <summary> /// This routine provides the event handler for the custom control text /// changed event. /// </summary> /// /// <param name="sender">Set to the sender of the event</param> /// <param name="e">Set to the event arguments</param> protected void ccAttributes_TextChanged(Object sender,    System.EventArgs e) { labMessage.Text = "Data Changed"; } //ccAttributes_TextChanged   } // CH06DisplayControlWithStateCS2 } 



ASP. NET Cookbook
ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
ISBN: 0596100647
EAN: 2147483647
Year: 2003
Pages: 202

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