Miscellaneous Topics


In this section, you'll see some useful techniques, depending on the types of server controls you write.

Accessing the ASP.NET Intrinsics

The Control class has a Context property that enables a server control to access ASP.NET intrinsic objects such as Application , Session , Request , Response , Error , and Cache . The Context property is defined as the System.Web.HttpContext type, which is the same type as the Page.Context property, which means it is used in the same way (such as for tracing).

The following code shows a simple server control that will output HTML to display the keys and values held in the application and session intrinsics:

  using System;   using System.Web;   using System.Web.UI;   using System.Web.UI.WebControls;   namespace WroxControls   {   public class IntrinsicDisplay : WebControl   {   protected override void Render(HtmlTextWriter writer)   {   writer.Write("<H3>Application Variables</H3>");   foreach(string key in Context.Application)   {   writer.Write("<BR>Key = '" + key +   "' , Value = '" +   Context.Application[key] + "'");   }     writer.Write("<H3>Session Variables</H3>");   foreach(string key in Context.Session)   {   writer.Write("<BR>Key = '" + key +   "' , Value = '" +   Context.Session[key] + "'");   }   }   }   }  

For example, the following ASP.NET page sets some application and session variables in the Page_Load event and declares an instance of the IntrinsicDisplay control:

  <%@ Register TagPrefix="Wrox" Namespace="WroxControls"   Assembly="Intrinsic" %>   <script runat="server" language="VB">   Sub Page_Load(sender as object, args as EventArgs)   Application("Name") = "Richard"   Application("Age") = "27"   Session("Year") = "1972"   Session("Car") = "Audi"   End Sub   </script>   <html>   <body>   <Wrox:IntrinsicDisplay runat="server" />   </body>   </html>  

Our control would render the output shown in Figure 18-32:

click to expand
Figure 18-32:

Writing Adaptive Controls

When writing a server control, the HTML (or other markup) that a control generates can be varied depending on the capabilities of the client browser. Using the Browser property of the Page object property, a control can determine the capabilities of the browser that is requesting the page. Most of the built-in ASP.NET server controls use this capability to reduce postback when a browser supports client- side JavaScript.

Here is a simple server control that determines the capabilities of a browser and reports some of them back to the client browser:

  using System;   using System.Web;   using System.Web.UI;   using System.Web.UI.WebControls;   using System.ComponentModel;   namespace WroxControls   {   public class AdaptiveControl: WebControl   {   override protected void Render(HtmlTextWriter objWriter)   {   objWriter.Write("<BR>");   objWriter.Write("Browser is: ");   objWriter.Write(Page.Request.Browser.Browser);     objWriter.Write("<BR>");   objWriter.Write("Browser version is: ");   objWriter.Write(Page.Request.Browser.Version);   objWriter.Write("<BR>");   if (Page.Request.Browser.JavaScript == true)   {   objWriter.Write("Browser supports JavaScript");   }   if (Page.Request.Browser.VBScript == true)   {   objWriter.Write("Browser supports VBScript");   }   if (Page.Request.Browser.Browser == "IE")   {   objWriter.Write("<BR>IE rocks!");   }   if (Page.Request.Browser.Browser == "Netscape")   {   objWriter.Write("<BR>Netscape rolls!");   }   }   }   }  

The output from this server control, when viewed in IE is shown in Figure 18-33:

click to expand
Figure 18-33:

The output from the same server control when viewed in a Mozilla browser is shown in Figure 18-34:

click to expand
Figure 18-34:

Using the HttpBrowserCapabilities class exposed through the Page.Browser property allows a server control to become adaptive. For example, if the Page.Request.Browser.JavaScript property returns true , a textbox control could generate some client-side JavaScript to perform an advanced client-side behavior “such as limiting the number or type of characters that can be entered.

Control Attributes

When developing server controls, you can annotate your classes with attributes that instruct the ASP.NET page compiler on how the control should be compiled and used. Attributes can also influence the way your control is displayed by a visual designer such as Visual Studio.NET.

The following table lists a few of the common attributes used by ASP.NET server controls:

Attributes

Description

Bindable

Determines whether a property should be displayed in the DataBindings dialog in VS.NET. The default value is false .

Category

If the property grid is sorted by category, this determines which category the property should appear in. The default value is Default .

DefaultValue

The default value of the property. The default value is the name of the control.

Description

The text describing the property. This appears at the bottom of the property grid in the Description box. The default value is String.Empty .

PersistenceMode

How (or whether) changes made to the value of the property should be persisted . The default value is EncodedDefaultInnerProperty .

Browsable

Determines whether a property is displayed in the designer. The default value is Yes .

TypeConverter, Editor

Hooks up extended UI for setting the property.

The following example shows the use of the Browsable property for hiding the SomeProperty from the properties window:

  [   Browsable(false),   ]   protected virtual string SomeProperty   {   get { ... }   }  

Custom Control Builders

A custom control builder is a class that derives from the class ControlBuilder , and overrides one or more methods that influence how a server deals with declaration within an ASP.NET page.

The following control builder class overrides the AllowWhitespaceLiterals method and returns false to indicate that spaces are not significant. If this method returned true , a LiteralControl containing spaces would be created and added to the Controls collection:

  public class NoWhiteSpaceBuilder: ControlBuilder   {   public override bool AllowWhitespaceLiterals()   {   return false;   }   }  

A control builder is associated with a given server control by making use of the ControlBuilder attribute:

  [   ControlBuilderAttribute(typeof(NoWhiteSpaceBuilder)),   Designer("System.Web.UI.Design.WebControls.XmlDesigner, " +   AssemblyRef.SystemDesign)   ]   public class SomeControl : Control {}  



Professional ASP. NET 1.1
Professional ASP.NET MVC 1.0 (Wrox Programmer to Programmer)
ISBN: 0470384611
EAN: 2147483647
Year: 2006
Pages: 243

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