i want loading dynamically usercontrol with jquery. first i create this UserControl in root website :
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UcProduct.ascx.cs" Inherits="UC_UcProduct" %>
<p> Mohsen</p>
after that i create .aspx page and write this code for loading UserControl
<head runat="server">
<title></title>
<script src="Script/jquery-1.7.1.min.js"></script>
<style>
body {
font-family: 'B Mitra', Tahoma, Arial;
font-size: 20px;
text-shadow: 4px 4px 4px #aaa;
}
</style>
<script>
$(function () {
$("#UserCtrl").load("UcProduct.ascx");
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="UserCtrl">
111
</div>
</form>
</body>
Thereafter i create class in App_code
namespace Eshop
{
public class jQueryHandler : IHttpHandler
{
public bool IsReusable
{
get { throw new NotImplementedException(); }
}
public void ProcessRequest(HttpContext context)
{
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;
}
}
}
finally add this section in web.config
<httpHandlers>
<add verb="*" path="*.ascx" type="Eshop.jQueryHandler,App_Code" />
</httpHandlers>
when run Default.aspx page don't load userControl , and when check with firebug i get this message please help me. thanks all.
I guess it is a problem of file extension. The server is not allowed to serve ascx files.
You may try :
<httpHandlers>
<remove verb="*" path="*.ascx"/>
<add verb="*" path="*.ascx" type="Eshop.jQueryHandler,App_Code" />
</httpHandlers>
or
declaring the handler for path="*.myascx"
, then load the corresponding .ascx
in the handler (this would change your ajax calls urls)