ASP.NET Tips & Tricks

Four Tips to Help You Save Time and Effort

asp:Feature

LANGUAGES: C# | VB.NET

ASP.NET VERSIONS: 1.x | 2.0

 

ASP.NET Tips & Tricks

Four Tips to Help You Save Time and Effort

 

By Chris D Agostino

 

It seems that at some point during the application development process I am faced with a unique programming challenge I haven t faced before. One lesson I ve learned over the years is that, while a situation may be unique to me, someone somewhere has already tackled a similar situation. As a result, I usually can find the solution, or at least get a pretty good handle on the problem, by simply doing some research. The purpose of this article is to point out some tips and tricks that will save you time down the road by giving you the answers now to some of the situations or issues you will likely face in the future.

 

Dynamically Loading User Controls Is Different in ASP.NET 2.0

Let s assume you have two menu user controls, named myMenu1.ascx and myMenu2.ascx, that you use throughout your Web application. On each page a user will be presented with either myMenu1 or myMenu2, depending on their role. Upon each Web page request, you only want to load into an ASP.NET panel control named Panel1 the menu with which the user is going to be presented.

 

To load the myMenu1 control if the user is a member of the Admin role, you could place the code listed in Figure 1 within the Web Form s Page_Init event handler.

 

VB

Dim strControl As String

If User.IsInRole("Admin") Then

   strControl = "myMenu1.ascx"

Else

   strControl = "myMenu2.ascx"

End If

Dim objcontrol As System.Web.UI.UserControl =

CType(Page.LoadControl(strControl),

 System.Web.UI.UserControl)

Panel1.Controls.Add(objcontrol)

 

C#

string strControl;

if (User.IsInRole("Admin"))

{

   strControl = "myMenu1.ascx";

}

else

{

   strControl = "myMenu2.ascx";

}

   System.Web.UI.UserControl objcontrol =

    ((System.Web.UI.UserControl)

     (Page.LoadControl(strControl)))

   ;

Panel1.Controls.Add(objcontrol);

Figure 1: Loading a user control dynamically in ASP.NET 1.x or ASP.NET 2.0.

 

This code will work in either ASP.NET 1.x or ASP.NET 2.0. However, let s assume that each control has a public Property named MenuTitle, which represents the title of the menu that will be dynamically displayed to the user. A different title will be displayed based on the user s role. To be able to access the property from within the Web Form, you must declare and instantiate an object as myMenu1 or myMenu2 instead of instantiating it as a generic user control. In ASP.NET 1.x all you need to do is place the code listed in Figure 2 within the Web Form s Page_Init event handler to get the job done.

 

VB

If User.IsInRole("Admin") Then

   Dim objcontrol As myMenu1 =

   CType(Page.LoadControl("myMenu1.ascx"), myMenu1)

   Panel1.Controls.Add(objcontrol)

   Objcontrol.MenuTitle = "This is the Admins Menu"

Else

   Dim objcontrol As myMenu2 =

   CType(Page.LoadControl("myMenu2.ascx"), myMenu2)

   Panel1.Controls.Add(objcontrol)

   Objcontrol.MenuTitle = "This is the non-Admins Menu"

End If

 

C#

if (User.IsInRole("Admin"))

   {

       myMenu1 objcontrol =

        ((myMenu1)(Page.LoadControl("myMenu1.ascx")));

       Panel1.Controls.Add(objcontrol);

       objcontrol.Title = "This is the Admins Menu";

   }

   else

   {

       myMenu2 objcontrol =

        ((myMenu2)(Page.LoadControl("myMenu2.ascx")));

       Panel1.Controls.Add(objcontrol);

       objcontrol.Title = "This is the non-Admins Menu";

   }

Figure 2: Dynamically loading user controls in ASP.NET 1.x.

 

With ASP.NET 2.0 there is an additional step. Because there are differences in the manner in which ASP.NET 2.0 and ASP.NET 1.1 compile assemblies, the code shown in Figure 2 will not work in ASP.NET 2.0. For every user control you may load dynamically on a Web Form, you must add a reference to it using the Reference directive on the aspx page. So, in addition to using the code listed in Figure 2, you must also add the directives listed below (following the @Page directive):

 

<%@ Reference Control="~/myMenu1.ascx" %>

<%@ Reference Control="~/myMenu2.ascx" %>

 

Referencing Master Page Properties or Methods

Master Pages in ASP.NET 2.0 allow you to define a common page layout and then reuse that layout throughout a Web site by implementing the Master Page in each required Web Form. You may also access a Master Page s public properties and methods from within a Web Form.

 

Let s assume you have a Master Page named MasterPage.master. This Master Page has a public property named FormTitle that allows you to dynamically set the title for the Web page that will be displayed to the user. You later implement the Master Page in a Web Form named ChildForm.aspx. To access the Master Page s public property you must first add a MasterType directive to the ChildForm s aspx page:

 

<%@ MasterType VirtualPath="~/MasterPage.master" %>

 

Once you add the directive, you may access the public properties and methods from within the Web Form s code-behind file by using the Master property. For instance, to set the SetTitle property you would use Master.FormTitle = My Form Title .

 

Note: You may have to build the application before IntelliSense will pick up the available methods and properties in the Master Page.

 

Look Out for the Master Page Bug

While developing a Web site that utilized Master Pages I would intermittently get the following error while trying to browse some of my Web Forms.

 

Unable to cast object of type ASP.masterpage_master to type ASP.masterpage_master

 

The error would randomly appear and go away after a while. I spent several hours trying to debug my code assuming that it must be an error on my part until I came across some postings on Microsoft s Web site explaining that this was a known issue that had to do with the way ASP.NET compiles the application. Microsoft is working on fixing this issue. In the meantime, you can remedy this problem by turning off batch compilation on the Web site. To do this, enter the following in the application s Web.Config file:

 

<compilation batch="false" />

 

Don t Use a code-behind File with the Global.asax

In ASP.NET 1.x it was perfectly normal to use the code-behind file for the Global.asax. However, using a code-behind file for the Global.asax in ASP.NET 2.0 can cause unpredictable results.

 

I recently upgraded a legacy .NET application to ASP.NET 2.0. During the upgrade process, the wizard left the code-behind file for the Global.asax intact. At first the application ran smoothly. But later, the code in the code-behind file stopped executing and I couldn t figure out why. I later found that, for this very reason, Microsoft recommends not using a code-behind file for the Global.asax.

 

You may notice that if you create a new Global.asax file from within an existing project, a code-behind file will not be created. In future updates I imagine Microsoft will fix the upgrade wizard so that it doesn t leave a separate code-behind file. In the meantime, after upgrading an ASP.NET 1.x application, do away with the separate code-behind file and use inline code for the Global.asax.

 

Conclusion

I hope these tips come in handy at some point during future development experiences, and that they save you some precious time and/or frustration down the road.

 

Chris D Agostino is an MCAD and owner of CJD Consulting, a company located in Portland, OR, that focuses on Web and application development. Chris has been developing Web applications for the last seven years. Find out more about him at http://cjdconsulting.com or e-mail him at mailto:[email protected].

 

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish