使用Handler动态加载ASP.NET控件不起作用

时间:2015-04-27 23:42:35

标签: javascript c# jquery asp.net httphandler

尝试使用自定义处理程序将用户控件动态加载到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。

我错过了什么?

感谢。

1 个答案:

答案 0 :(得分:0)