Load Master Page User Control Dynamically to Improve Performance

Introduction

Problem: We have the master page and we will embed the user the control and the same master page will used by various pages. For some pages we don't require the user control to be visible. If we also hide the call to that user control, we cannot avoid it.

Step 1 : We have very simple user control, just some label in HTML.

master1.gif

Step 2 : In code behind, we will hold the thread for a while, assuming we are doing some complex process in that user control and display the content.

master2.gif

Step 3 : Add the user control to the master page. First register it and using the tag name embed the control.

master3.gif

Step 4 : Just create a new ASP.Net page with master page reference and load it and the result will be as below. Ignore the design aspects; we will focus on the user control alone.

master4.gif

The User Control will be loaded in right side and below the load time as well.

Step 5 : We will create one more page, that doesn't require the user control. We will just hide the control using jQuery.

master5.gif
Even if we hide also, the user control will be called internally.

master6.gif

Step 6 : We will remove the user control related "HTML" in the master page and just add a placeholder.

master7.gif

Step 7 : Now we can load the master page control from the child/content page. Write the following code in the Page_LoadComplete event of the Content Page, which comes just after the Page_Load event of the Master Page.

master8.gif
We will run the page and get the same result as below.

master9.gif

Now we will load the other page which doesn't require the Master Page User Control. It just loads the master and child page contents alone.

master10.gif

Summary

We need to add some code in the page which requires loading the Master Page User Control dynamically.


Similar Articles