Using the LoadControl Method

   

Using the LoadControl Method

One more very cool feature of User controls (and a major yucky of SSIs) is that they can be dynamically loaded with logic. This isn't to say that you can't dynamically execute the code in SSIs. This you can do as follows:

 <%if var = 1 then%>  <!--#include file="file1.inc" -->  <%else%>  <!--#include file="file2.inc" -->  <%end if%> 

If var is equal to 1, then the code that is in file1.inc executes; otherwise the code in file2.inc executes. The thing to remember is that all the contents of both files will be included in the file, though. So if you have 10 include files with 200 lines of code each on your page, and only one of these will execute, you have a total of 2000 lines of code, 1800 of which aren't needed and just bog down your server.

The Loadcontrol method allows you to truly load User controls dynamically and treat them as objects and plop them on the page wherever you see fit.

In the following example, the value of an item in the QueryString is requested, and, based on that value, one control or another is loaded. This example demonstrates how you might dynamically load menus to different types of users. You would probably never use the QueryString to pass security or user information like this, but it is simple and easy to see for this example.

Following are two User controls, which are simply an HTML table containing different menu items. A known user will receive two more items than an anonymous user does. We control this by maintaining two different User controls that have this specific HTML in them. I am going to display the User control example code only once because it is identical for both languages.

Anonymous User uc_loadcontrol_au_vb.ascx / uc_loadcontrol_au_cs.ascx
<table width="200" border="0" cellspacing="0" cellpadding="0">  <tr>  <td bgcolor="#000000">  <div align="center" style="color:#FFFFFF">Anonymous User</div>  </td>  </tr>  <tr>  <td bgcolor="#CCCCCC">  <div align="center"><a href="#">Menu Item 1</a></div>  </td>  </tr>  <tr>  <td bgcolor="#CCCCCC">  <div align="center"><a href="#">Menu Item 2</a></div>  </td>  </tr>  <tr>  <td bgcolor="#CCCCCC">  <div align="center"><a href="#">Menu Item 3</a></div>  </td>  </tr>  </table> 
Known User uc_loadcontrol_ku_vb.ascx / uc_loadcontrol_ku_cs.ascx
<table width="200" border="0" cellspacing="0" cellpadding="0">  <tr>  <td bgcolor="#000000">  <div align="center" style="color:#FFFFFF">Known User</div>  </td>  </tr>  <tr>  <td bgcolor="#CCCCCC">  <div align="center"><a href="#">Menu Item 1</a></div>  </td>  </tr>  <tr>  <td bgcolor="#CCCCCC">  <div align="center"><a href="#">Menu Item 2</a></div>  </td>  </tr>  <tr>  <td bgcolor="#CCCCCC">  <div align="center"><a href="#">Menu Item 3</a></div>  </td>  </tr>  <tr>  <td bgcolor="#CCCCCC">  <div align="center"><a href="#">Menu Item 4</a></div>  </td>  </tr>  <tr>  <td bgcolor="#CCCCCC">  <div align="center"><a href="#">Menu Item 5</a></div>  </td>  </tr>  </table> 
Visual Basic .NET uc_loadcontrol_vb.aspx
<%@ page language="vb" EnableViewState="false" runat="server"%>  <script runat=server>  Sub Page_Load()      dim myControl as Control      If lcase(Request.Params("user")) = "known" then          myControl = Page.LoadControl("uc_loadcontrol_ku_vb.ascx")      else          myControl = Page.LoadControl("uc_loadcontrol_au_vb.ascx")      end if      myPlaceHolder.Controls.Add(myControl)  end sub  </script>  <html>  <title>User Controls -  Load Control</title>  <body>  <asp:PlaceHolder runat="server"  />  </body>  </html> 
C# uc_loadcontrol_cs.aspx
<%@ page language="cs" EnableViewState="false" runat="server"%>  <script runat=server>  void Page_Load(){      Control myControl;      if (Request.Params["user"] == "known"){          myControl = Page.LoadControl("uc_loadcontrol_ku_vb.ascx");      }else{          myControl = Page.LoadControl("uc_loadcontrol_au_vb.ascx");      }      myPlaceHolder.Controls.Add(myControl);  }  </script>  <html>  <title>User Controls -  Load Control</title>  <body>  <asp:PlaceHolder runat="server"  />  </body>  </html> 

As you can see in Figures 5.6 and 5.7, the correct menu was loaded for the appropriate users. This is one simple example of how using the LoadControl for User controls can give you additional power to deliver user-specific content in your ASP.NET web applications. And power is good, I always say.

Figure 5.6. When there is no QueryString value, the User control for Anonymous Users is loaded.
graphics/05fig06.gif
Figure 5.7. When you identify the user as known, the User control for Known Users is delivered and loaded.
graphics/05fig07.gif

Now look at the line of code that reads as follows:

 myControl = Page.LoadControl("uc_loadcontrol_ku_vb.ascx"); 

This line uses the Page.LoadControl method to create a User control object named myControl. Two of these statements populate myControl with one of the two files, based on a value in the QueryString called user. If user=known, then we load for known users; otherwise, we load the User control for anonymous users. Look at the next relevant line:

 myPlaceHolder.Controls.Add(myControl); 

Down in the body of the .aspx page is a generic placeholder object that is just waiting for something to be added to it. You do just that with the preceding line of code by using the Control.Add() method. This is a great way of dynamically controlling what control, or even whether a control, should be displayed.


   
Top


ASP. NET for Web Designers
ASP.NET for Web Designers
ISBN: 073571262X
EAN: 2147483647
Year: 2005
Pages: 94
Authors: Peter Ladka

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net