2013-02-08 53 views
1

我想用jquery動態加載用戶控件。首先,我創建這個用戶控件在根網站:用jQuery加載用戶控件與處理程序

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UcProduct.ascx.cs" Inherits="UC_UcProduct" %> 
<p> Mohsen</p> 

之後,我創建.aspx頁面中並寫加載用戶控件的代碼

<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,當用螢火蟲檢查時我得到這個訊息 enter image description here 請幫幫我。謝謝大家。

回答

1

我想這是一個文件擴展名的問題。服務器不允許用作ascx文件。

你可以試試:

<httpHandlers> 
    <remove verb="*" path="*.ascx"/> 
    <add verb="*" path="*.ascx" type="Eshop.jQueryHandler,App_Code" /> 
</httpHandlers> 

聲明處理程序path="*.myascx",然後加載在處理程序中相應的.ascx(這會改變你的Ajax調用的URL)

相關問題