尝试使用自定义处理程序将用户控件动态加载到aspx页面中。我正在关注http://blog.ovesens.net/2008/12/dynamically-loading-asp-net-user-controls-with-jquery/
中的示例public class AjaxUserControlHandler : AjaxControlHandler
{
public override Control GetControl(HttpContext context)
{
// Get the path to the user control
string path = context.Request.Url.LocalPath;
using (var page = new Page())
{
var viewControl = page.LoadControl(path) as UserControl;
return viewControl;
}
}
}
using System.Web;
using System.Web.UI;
public class AjaxUserControlHandler : AjaxControlHandler
{
public override Control GetControl(HttpContext context)
{
// Get the path to the user control
string path = context.Request.Url.LocalPath;
using (var page = new Page())
{
var viewControl = page.LoadControl(path) as UserControl;
return viewControl;
}
}
}
using System;
using System.ComponentModel;
public class AjaxEnabledAttribute : Attribute
{
[DefaultValue(RequestMethodSupport.All)]
public RequestMethodSupport Method { get; set; }
}
public enum RequestMethodSupport
{
All,
GET,
POST
}
我的欢迎Portlet
[AjaxEnabled]
public partial class WelcomePortlet : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
在我的Web.Config
中<httpHandlers>
<remove verb="*" path ="*.ascx"/>
<add verb="*" path="*.ascx" type="AjaxUserControlHandler"/>
</httpHandlers>
在我的aspx页面中。
$("#placeholder").load("Controls/WelcomePortlet.ascx");
但是控件没有加载。我在AjaxUserControlHandler和AjaxUserControlHandler中放置了断点,代码无法运行。
注意:AjaxControlHandler,AjaxEnabledAttribute和AjaxUserControlHanlder位于App_Code文件夹中,并且已标记为Compile而不是Content。
我错过了什么?
感谢。
答案 0 :(得分:0)
您使用的是哪个版本的IIS?您还需要在Web.config中配置IIS处理程序映射或WebServer部分。
https://msdn.microsoft.com/en-us/library/bb515343(v=vs.140).aspx http://www.iis.net/learn/develop/runtime-extensibility/developing-iis-modules-and-handlers-with-the-net-framework