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
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!