Dynamically Loading User Controls in ASP.NET

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 15
November 11, 2001
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Dynamically Loading User Controls


One of the coolest things about ASP.NET is User Controls. These are a "Include like" file that allows a developer separate code from the .ASPX page. Another very powerful thing is the re-usable code UserControls offer. Recently on ASPFree.com, I (Steve Schofield) wanted to provide a feedback form on certain articles. The challenge was to show the form on only selected web-pages. To help understand what I challenged with and the solution that I came up and is so simple yet very powerful. ASPFree.com uses two global UserControls to control the layout of each aspx page (toppage.ascx, bottompage.ascx).

Standard Template used on ALL .aspx pages on ASPFree.com

<%@ Page Language="VB" EnableSessionState="False" EnableViewState="True" 

Trace="False" Debug="False" Strict="True" %>
<%@ Register TagPrefix="Top" TagName="TopN" Src="toppage.ascx" %>
<%@ Register TagPrefix="Bottom" TagName="BottomN" Src="bottompage.ascx" %>

<Top:TopN TitleOfDemo="Enter Title Here" runat="server" />


<Bottom:BottomN ShowFeedBack="No" runat="server" />

BAD DESIGN in the IF/THEN statement

Within the bottompage.ascx file I placed an IF/THEN statement to read the ShowFeedBack Property. This technique worked fine and the form only was displayed on each page I set the Property ShowFeedBack to Yes. This is the original code I started out with.

<!--BAD CODE DESIGN-->
<script language="vb" runat="server">
Public ShowFeedBack as String
</script>

<%
If ShowFeedBack = "Yes" Then
%>
<%@ Register TagPrefix="Tag" TagName="Feedback" Src="feedback.ascx" %>
<Tag:Feedback runat="server" />
<%
End If
%>

There was one major flaw with this technique. No matter if the form was displayed or not, the uc_form.ascx controls were rendered on the ASPX page. This was a major design flaw and would hinder performance on the page and potentially cause other problems.

To view the pages that show the control tree, notice all the controls that are rendered on both pages. (SHOWFEEDBACK property set to Yes) and (SHOWFEEDBACK property set to No). Notice the page set to No doesn't have the form showing but yet the control from the uc_form.ascx where rendered.

Good DESIGN in the IF/THEN statement

<!--Will Show Feedback form if property set to yes-->
<script language="VB" runat="server">

'Define ShowFeedback property
Public ShowFeedBack as string

Protected Sub Page_Load(Source as Object, E as EventArgs)
If ShowFeedBack = "Yes" Then
Dim uc As Control = Page.LoadControl("uc_form.ascx")
myPlaceHolder.Controls.Add(uc)
End If
End Sub
</script>
<asp:PlaceHolder runat="server" id="myPlaceHolder" />

Using a Placeholder tag, this allowed the page to dynamically load the control if the Property value was set to Yes. This was a very minor adjustment in my code yet providing a correct design. This technique provided other benefits too, several pages on ASPFree.com didn't have the ShowFeedBack Property even declared. I thought there would be a performance hit or problems by adding this property to new pages. This technique allowed for adding the property on new pages and not having to go back and adjust the existing pages.

To correctly show how this technique worked, view the pages that show the Control Tree section, notice all the controls that are rendered on both pages. (SHOWFEEDBACK property set to Yes) and (SHOWFEEDBACK property set to No). Notice the page set to No doesn't have the form showing and the controls from the uc_form.ascx where NOT rendered. This is a very simple but powerful method of control both performance and flexibility of a very large site as ASPFree.com. Hopefully found this useful!

Steve Schofield
steve@aspfree.com

Webmaster
http://aspfree.com

blog comments powered by Disqus
ASP.NET ARTICLES

- Implementing ASP.NET 4.0 Page.MetaDescriptio...
- ASP.Net Development Tips
- Intro to Sessions in ASP.Net
- Google Maps API Introduction in ASP.NET usin...
- Creating an ASP.NET 3.5 Gridview Image Galle...
- Encrypt QueryString in ASP.NET 3.5 using VB....
- ASP.NET 3.5 Drop Down List Controls
- Connect to Access Database with ASP.Net
- Secure Audio Streaming with ASP.Net and Flash
- Dynamic Sitemap and Navigation in ASP.Net
- Implement Gzip and Deflate Compression in AS...
- Run ASP.Net in Ubuntu with Apache
- ASP.Net Mono Website Contact Forms
- ASP.Net URL Rewriting Methods
- Murach`s ASP.NET 4 Web Programming with C# 2...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials