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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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: 4 stars4 stars4 stars4 stars4 stars / 10
    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! Try the IBM SOA Sandbox for Connectivity

    Visit IBM developerWorks to try the IBM SOA Sandbox for connectivity. The SOA Sandbox for connectivity provides a trial environment with the tooling and components to help you explore how to effectively connect your infrastructure and integrate all of the people, processes and information in your company. Use the hosted sandbox to explore SOA techniques that streamline connecting existing IT assets together, as well as learn how to connect them to new business logic.
    FREE! Go There Now!


    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! Info 2.0: Harnessing the power of Web 2.0 and Enterprise Mashups

    Listen to this webcast to get an overview of Info 2.0 and a technical demo of how to quickly build an enterprise mashup. IBM's Info 2.0 technology leverages emerging Web 2.0 technologies such as mashups, feeds, AJAX, and JSON in order to simplify assembly of information using feeds and services. Come learn about the technical elements of Info 2.0 including the Feed Generation framework, Mashup Engine, and mashup assembly components. Learn how to pull information from databases, departmental information, and the Web to create mashups critical to your company’s success. We will also discuss best practices to help you get started.
    FREE! Go There Now!


    Role of Integrated Requirements Management in Software Delivery

    As organizations integrate software into every aspect of business, they are constantly pressured to deliver faster, better, and cheaper results. Unfortunately, a “dis-integrated” software delivery approach reduces returns while increasing costs. This IBM Rational White Paper shows how Integrated Requirements Management aligns organizations around maximizing value and keeping pace with change.
    FREE! Go There Now!


    NEW! "ebook: Exploring IBM SOA Technology & Practice

    Learn field-tested SOA principles, methodology, technology and implementation from the global SOA market leader - in a new e-book by an IBM SOA expert. Written by IBM Certified SOA Solution Designer Bobby Woolf, "Exploring IBM SOA Technology & Practice" is the ultimate insider's guide to SOA - a PDF e-book packed cover to cover with IBM's specific advice on how to make your SOA implementation a success.
    FREE! Go There Now!


    NEW! Maintaining QoS and Process Integrity in an SOA Environment

    This webcast outlines the best practices that must be instituted to gain the maximum benefit from SOA while maintaining high quality of service. Whether you are deploying new applications or managing and monitoring your existing infrastructure, learn how you can ensure high quality of services with SOA based solutions from IBM. All registrants who attend this live Web Seminar will receive complimentary access to a white paper titled “Maintaining QoS in an SOA Environment”.
    FREE! Go There Now!


    NEW! Build Web services with transport-level security using Rational Application Developer V7, Part 1: Build Web services and Web services clients

    Build secure Web services with transport-level security using IBM Rational Application Developer V7 and IBM WebSphere Application Server V6.1. Follow this three-part series for step-by-step instructions about how to develop Web services and clients, configure HTTP basic authentication, and configure HTTP over SSL (HTTPS). This first part of the series walks you through building a Web service for a simple calculator application. You generate and test two different types of Web services clients: a Java Platform, Enterprise Edition (Java EE) client and a stand-alone Java client. You also handle user-defined exceptions in Web services.
    FREE! Go There Now!


    NEW! Achieving True Agility -- How process can change the behavior of your tools

    Achieving true agility is a never-ending effort. We will showcase how you can become agile incrementally, a few practices at the time.Which practices should any agile team strive to adopt? What additional practices should you consider based on your needs to scale? Adopting practices are however made much easier with the right tool support. What about if your tools adapt to your practices? We will take a look at how the Jazz technology can be leveraged to make your process change the behavior of your tools.
    FREE! Go There Now!


    NEW! Applying lean thinking to the governance of software development

    Effective governance for lean development isn’t about command and control. Instead, the focus is on enabling the right behaviors and practices through collaborative and supportive techniques. Hear from Scott Ambler on how it is far more effective to motivate people to do the right thing than it is to force them to do so. Learn how to form a lightweight, collaboration-based framework that reflects the realities of modern IT organizations.
    FREE! Go There Now!


    NEW! Evaluate IBM Lotus Sametime Standard V8.0

    Visit IBM developerWorks to download a free trial of the latest release of IBM Lotus Sametime Standard V8.0. Lotus Sametime Standard V8.0 is a platform for unified communications and collaboration that combines security features with an extensible, open solution including integrated Voice over IP, geographic location awareness, mobile clients, and a robust Business Partner community offering telephony and video integration.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    ASP.NET ARTICLES

    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT