User Control – Reusing across projects, Loading async

Posted: November 22, 2010 in ASP.NET
Tags: , ,

Create custom controls from existing user controls

ASP.NET has always supported two different ways of authoring server controls:

  1. Custom controls: These are controls written from scratch exclusively using code. Essentially, you write a class that extends Control (directly or indirectly), and you take care of everything. You need to write logic to create child controls that you want to use, and you override the Render method to perform rendering. Typically, you build this control into a redistributable assembly (that is, a DLL), which can then be used by any ASP.NET applications, simply by placing the assembly in the “bin” directory of the application (or in the Global Assembly Cache).
  2. User controls: These controls are written using an .ascx file, which looks much like an aspx page. That is, you can simply drag and drop the UI elements that you want to use into the design surface. You don’t need to worry about control creation, nor about overriding the Render method.

Article in below link by David Ebbo shows how we can have the best of both worlds by turning an ascx user control into a redistributable custom control, by making use of the new ASP.NET precompilation features

http://msdn.microsoft.com/en-us/library/aa479318.aspx 

Below link gives another approach which may not be that interesting …

http://webproject.scottgu.com/CSharp/UserControls/UserControls.aspx

Getting UserControl response – useful when loading section of page in AJAX call

StringBuilder sb = new StringBuilder();
UserControl uc = new UserControl();
Overlay ctrl = uc.LoadControl(“~/controls/Overlay.ascx”) as Overlay;
ctrl.SetInfo();
ctrl.RenderControl(new HtmlTextWriter(new StringWriter(sb)));
return sb.ToString();

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s