ASP.NET
  Home arrow ASP.NET arrow Dynamically Loading User Controls in ASP.N...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Dynamically Loading User Controls in ASP.NET
By: aspfree
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 11
    2001-11-11

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More ASP.NET Articles
    More By aspfree

     

    IBM® developerWorks developerWorks - FREE Tools!


    NEW! Application Development Tools for the Mainframe Developer

    You probably have thousands of lines of COBOL code loaded with business intelligence and being used to run your business, along with an army of developers maintaining these applications. Learn how to prepare your applications and developers so you can keep that competitive edge and move to a service-oriented architecture with the IBM Rational Enterprise Modernization solutions. Replay is available for 9 months.
    FREE! Go There Now!


    NEW! Discovering the value of WebSphere Process Server

    WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies.
    FREE! Go There Now!


    NEW! Don't wait! Try the Rational Application Developer (RAD) v7.5 open beta code today

    Download the Rational Application Developer (RAD) v7.5 open beta code and start developing applications for the JEE5 standard which features EJB3.0, JPA, JSF 1.2, JSP 2.1 and Servlet 2.5 standards. When you use this beta you will see how you can increase developer productivity for already existing applications with improved support for refactoring, as well as adding new features to existing applications. In addition, the beta provides tooling for JD Edwards, Oracle, SAP, Siebel and PeopleSoft to improve the developer productivity with these enterprise systems.
    FREE! Go There Now!


    NEW! Download the free Web Application Security eKit

    Discover how IBM Rational AppScan Standard Edition can help you detext vulnerabilities in your web applications in the Web Application Security eKit. IBM Rational AppScan is a leading suite of automated web application security solutions that scan and test for common Web application vulnerabilities. The new Web Application Security eKit provides you with valuable resources, including white papers, demos, and additional information on the benefits of testing your Web applications.
    FREE! Go There Now!


    NEW! Evaluate IBM Rational Developer for System i V7.1

    Download a free trial version of IBM Rational Developer for System i V7.1, which provides a complete development environment for traditional i5/OS application development. IBM Rational Developer for System i is a new eclipse-based workstation offering for i5/OS application development that provides a comprehensive Integrated Development Environment for edit/compile/debug of traditional RPG/COBOL/C/C++ i5/OS applications.
    FREE! Go There Now!


    NEW! Section 508 of the U.S. Rehabilitation Act: Web accessibility compliance

    Because access to government information continues to be an area of concern for many U.S. citizens with disabilities, the U.S. government enacted Section 508 of the Rehabilitation Act in 2001 to ensure that government agencies create accessible Web content, enabling all citizens to access the information they need. A fully accessible Web site makes Web content accessible to all individuals, including those with disabilities, who may be accessing Web content via a variety of user agents. Common user agents include standard Web browsers, text-only browsers, assistive devices and mobile devices such as cell phones or personal digital assistants (PDAs).
    FREE! Go There Now!


    NEW! Trial download: IBM Rational Tester for SOA Quality V7.0.1

    Get a free trial download of the latest version of IBM Rational Tester for SOA Quality V7.0.1, a functional and regression testing tool that enables the creation, comprehension, modification and execution of testing GUI-less Web services.
    FREE! Go There Now!


    NEW! Try the IBM SOA Sandbox for Process

    Visit IBM developerWorks to try the IBM SOA Sandbox for process. The SOA Sandbox for process focuses on providing a trial environment with the necessary tooling and components required to gain a better understanding of business processes and how to best improve existing business processes to derive value quickly.
    FREE! Go There Now!


    NEW! Webcast: IBM Rational Build Forge - Beyond the Build

    The discipline of assembling and delivering software is maturing beyond standard developer-centric compile/test software builds. The end-to-end software development lifecycle is emerging as the new focus moves “Beyond the Build.” Join this on demand webcast to learn about methods for streamlining software delivery and key capabilities of the IBM Rational Build Forge framework for automating build and release management in environments of any size.
    FREE! Go There Now!


    NEW! Webcast: Quickly provide customized, integrated user interfaces with Lotus Notes 8

    IBM Lotus Notes 8 provides a wide range of developers the ability to provide customized, integrated user interfaces via composite applications and via custom sidebar and toolbar plug-ins. This webcast provides you with tips and techniques to use with out-of-the-box capabilities of Lotus Notes 8, and survey how you can share useful components within your own company and within a larger community.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    ASP.NET ARTICLES

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek