Iver's web place

Life is a journey ... taken one shot at a time!

How to create custom controls in ASP.NET part 2 of 3

State Management

Previusly in the last post we saw the web custom controls attributes. We can interactive with the web user control through its attributes.
To implement a custom control, you have to implement a class in procedural lenguages as C# or VB.NET that derives from a base class such as Control. Every server control directly or indirectly derives from the Control base class.
The base class Control provides server controls with the infrastructure they need to operate as a server control in the ASP.NET framework. One of these infrastructural methods is a method named Render. This method is where a server control generates or renders its HTML markup text.
For example, if you wish to put a html table into the page, you need to use the next code or similar code:

namespace CustomComponents
{
   public class CustomControl : Control
   {
       protected override void Render (HtmlTextWriter writer)
       {
           writer.Write("<table style='border: solid 1px black; width:250' id='myTable'> ");
           writer.Write("<tr><td> Table content</td></tr>  ");
           writer.Write("</table> ");
           base.Render(writer);
       }
   }
}
 
The HtmlTextWriter subclass of the TextWriter base class, is specifically designed to write a stream of HTML markup characters into a server control output's stream. With the previous Render Method we can put the HTML code into web page but when we pass strings values to the Write method of the HtmlTextWriter class, strings pose the following problems:
  • The compilers don't catch the typing errors. For example, the following error:
  Writer.Write("<spon/>");
 
  • You must emit different HTML markup text for different browsers.
For resolve all these problems exists HtmlTextWriterTag, HtmlTextWriterAttribute and HtmlTextWriterStyle enumerations. For example, HtmlTextWriterTag.Span corresponds to the HTML tag, HtmlTextWriterAttribute.Id corresponds to the id HTML attribute and HtmlTextWriterStyle.Width corresponds to the width CSS style attribute.
These enumerations provide the following benefits:
  • We can benefit from the compiler type-checking capability to avoid problems associated with string manipulations.
  • Visual Studio provides IntelliSense support for these enumerations.
  • The ASP.NET Framework comes with two versions of these enumerations, the HTML 4.0 standard and the HTML 3.2 standard version. The custom control doesn't have to do extra work to create different HTML markup texts for different browsers, the ASP.NET Framework will automatically use the right versions.
Now, with the attibute enumerations our code would be like this:
namespace CustomComponents
{
   public class CustomControl : Control
   {
     protected override void Render(HtmlTextWriter writer)
        {
            writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, "solid");
            writer.AddStyleAttribute(HtmlTextWriterStyle.BorderColor, "black");
            writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "1");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "250px");
            writer.AddAttribute(HtmlTextWriterAttribute.Id, "myTable");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Table content");
            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderEndTag();
            base.Render(writer);
        }
   }
}
 
Recall that the main goal of the Render method is to generate the HTML markup text that displays your control on the client browser.
Now, if the properties of our custom control need to maintain their values across page postbacks, you have to take extra steps.
The Control class exposes a collection property named ViewState. This collection automatically saves and loads its content across page postbacks without any coding on your part. You should use this collection as the backing store for those properties of your custom control that need to mantain their values across page postbacks.
The properties of your custom controls as items of type System.Object regardless of their real types, and returns these values as items of type System.Object. That is why the getter of each property must convert (type cast) the item that it retrieves from the ViewState collection to the real type of the property before you returns the value.
When it comes to storing objects to ViewState we must keep the following two important things in mind:
  • ViewState is optimized to convert certain types such as System.String, System.Int32, System.Boolean, System.Drawing.Color, System.Unit and HashTable, Array and ArrayList of Int32, Boolean, Color and Unit values. If the custom control needs to store other types to ViewState, we should write a custom type converter that optimized to convert the custom type to its string representation.
  • Because the string representations of the objects we add to ViewState are stored on the ASP.NET page, we must store only necesary information to ViewState to reduce the size of the ASP.NET page.
Also, we have the control state, introduced in ASP.NET version 2.0. If you wish you can disable view state for the page or for an individual control for performance. However, control state cannot be disabled. On postback, ASP.NET deserializes the contents of the hidden element and loads control state into each control that is registered for control state.
An sample of control state:
       protected override void LoadControlState(object savedState)
        {
            object[] objState = (object[])savedState;
            base.LoadControlState(objState[0]);
            this.EnableRowClickEvent = (bool)objState[1];
            this.EnableRowDoubleClickEvent = (bool)objState[2];
            this.ActivateRollOverColor = (bool)objState[3];
        }
        protected override object SaveControlState()
        {
            object[] objR = {
                              base.SaveControlState(),
                              this.EnableRowClickEvent,
                              this.EnableRowDoubleClickEvent,
                              this.ActivateRollOverColor
                            };
            return objR;
        }
 
When you override the SaveControlState base method, you can persist the base object result from the base SaveControlState method, and after you can load in the LoadControlState method. Control state is designed for storing a control's essential data (such as a pager control's page number) that must be available on postback to enable the control to function even when view state has been disabled. By default, the ASP.NET page framework stores control state in the page in the same hidden element in which it stores view state. Even if view state is disabled, or when state is managed using Session, control state travels to the client and back to the server in the page.
Resources
Professional ASP.NET 2.0 Server Control and Component Development
Dr. Shahram Khosravi
ISBN-13: 978-0-471-79350-2
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0471793507.html

http://msdn.microsoft.com/es-es/library/system.web.ui.htmltextwriter(VS.80).aspx

Trackback URI: http://en.iver.com.mx/index.php?trackback/2

#1 Re: How to create custom controls in ASP.NET part 2 of 3

dan09, <> / 23 February 2009  
avatar

Me parece correcto tu forma de enseñar esto de generar custom controls in asp.net.
saludos

[ Reply (0) ]

#2 Re: How to create custom controls in ASP.NET part 2 of 3

mhvishal, <mhvishal(at)yahoodotin> / 19 May 2009  
avatar

Just like ViewState, dynamic controls seem to be fodder for much debate, and a source of many confusing issues. This article will be the first of a multi-part series to detail just about everything you could ever want to know about how Dynamic Controls

[ Reply (0) ]

#3 Re: How to create custom controls in ASP.NET part 2 of 3

ankara nakliye, <ankaranakliyecm(at)hotmaildotcom> / 08 July 2010  
avatar

wery nice web site thank you :d

[ Reply (0) ]

#4 Re: How to create custom controls in ASP.NET part 2 of 3

saç ekimi, <saceki(at)gmaildotcom> / 15 July 2010  
avatar

thanks

[ Reply (0) ]

Leave a Comment

Write the captcha code you are seeing.

Comment XML feeds: RSS | Atom

Blog Calendar

July 2010
Sun Mon Tue Wed Thu Fri Sat
27 28 29 30 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
1 2 3 4 5 6 7