我想用 jquery 动态加载用户控件。首先我在根网站中创建这个 UserControl:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UcProduct.ascx.cs" Inherits="UC_UcProduct" %>
<p> Mohsen</p>
之后,我创建 .aspx 页面并编写此代码以加载 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>
此后我在 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;
}
}
}
最后在 web.config 中添加此部分
<httpHandlers>
<add verb="*" path="*.ascx" type="Eshop.jQueryHandler,App_Code" />
</httpHandlers>
运行 Default.aspx 页面时不加载 userControl ,当与 firebug 检查时,我收到此消息, 请帮助我。谢谢大家。