Click here to Skip to main content
15,881,248 members
Articles / Web Development / ASP.NET

Load ASP.Net User Control Dynamically Using jQuery

Rate me:
Please Sign up or sign in to vote.
4.62/5 (6 votes)
12 Oct 2010CPOL2 min read 77.5K   18   7
Today we will explore the way of loading ASP.Net user control at run time using jQuery. jQuery has one method load(fn) that will help here. This load(fn) method has following definition.

Today we will explore the way of loading ASP.Net user control at run time using jQuery. jQuery has one method load(fn) that will help here. This load(fn) method has following definition.

load (url, data, callback): A GET request will be performed by default – but if any extra parameters are passed, then a POST will occur.
url (string): URL of the required page
data (map – key/value pair): key value pair data that will be sent to the server
callback (callback method): call back method, not necessarily success

Now comes custom HttpHandler that will load the required user control from the URL given by this load(fn) method. We all know that it is either in-built or custom HttpHandler that is the end point for any request made in ASP.Net.

Let’s see by example. In the ASP.Net application, add one aspx page and user control. Then, add one more class derived from IHttpHandler. The aspx html markup will look something like this.

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat=""server"">
<title>Load ASP.Net User Control</title>
<script src="jquery-1.2.6.js"></script>
<script>
$(document).ready(function() {
$("#BtnLoadUserCtrl").click(function() {
$("#UserCtrl").load("SampleUserCtrl.ascx");
});
});
</script>
</head>
<body>
<form runat=""server"">
<div>
<br />
<input value="Load User Control" /> <br />
<div id="UserCtrl"></div>
</div>
</form>
</body>
</html>

The code is quite readable. On the click event of BtnLoadUserCtrl button, SampleUserCtrl.ascx user control is being tried to load in the <div> element having id UserCtrl.

Then, write our custom Httphandler called jQueryHandler as below.

public class jQueryHandler:IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
// We add control in Page tree collection
using(var dummyPage = new Page())
{
dummyPage.Controls.Add(GetControl(context));
context.Server.Execute(dummyPage, context.Response.Output, true);
}
}

private Control GetControl(HttpContext context)
{
// URL path given by load(fn) method on click of button
string strPath = context.Request.Url.LocalPath;
UserControl userctrl = null;
using(var dummyPage = new Page())
{
userctrl = dummyPage.LoadControl(strPath) as UserControl;
}
// Loaded user control is returned
return userctrl;
}

public bool IsReusable
{
get { return true; }
}
}

Do not miss to add this HttpHandler in the web.config.

<httpHandlers>
<add verb="*" path="*.ascx" type="JQUserControl.jQueryHandler, JQUserControl"/>
</httpHandlers>

This web.config configuration tells that jQueryHandler will process request for file type having .ascx extension and methods all (GET, POST, etc). The type attribute value is something like:
type=”Namespace.TypeName, Assembly name where Handler can be found”

Now we are ready to test our sample. Run the page, and see on the click of button, the sampleusertCtrl.ascx is loaded.

I hope we can now extend this concept to fit any such programming requirement in future.
Happy Coding!


Posted in .Net Technologies, ASP.Net, C#/VB.Net, CodeProject, Dot Net Tips, JavaScript/jQuery/JSON/Ajax Tagged: .Net3.5, ASP.Net, HttpHandler, jQuery, UserControl

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Imfinity India Pte Ltd, Noida (Excelsoft Company)
India India
http://www.imfinity.com/

Comments and Discussions

 
Questionerror 404.7 when run localhost Pin
trieuquang198824-Jul-13 9:12
trieuquang198824-Jul-13 9:12 
QuestionGET URL 404 (Not Found) Pin
Nafiseh Salmani2-Apr-13 5:52
Nafiseh Salmani2-Apr-13 5:52 
BugProblem With IIS 7.0 and jQuery Handler Pin
v_farmak5-Mar-13 4:11
v_farmak5-Mar-13 4:11 
GeneralRe: Problem With IIS 7.0 and jQuery Handler Pin
Dinesh K Mandal5-Mar-13 20:00
professionalDinesh K Mandal5-Mar-13 20:00 
QuestionUsing this technique in parallel with standard UserControl usage Pin
Member-47222599-May-12 20:14
Member-47222599-May-12 20:14 
AnswerRe: Using this technique in parallel with standard UserControl usage Pin
Dinesh K Mandal10-May-12 3:22
professionalDinesh K Mandal10-May-12 3:22 
GeneralMy vote of 2 Pin
Nathan St12-Oct-10 3:20
Nathan St12-Oct-10 3:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.