一直不知道怎么把一个现成的gridview通过ajax的方式调用到页面上,后来看了老赵(这哥们很V5)很早之前的一篇博客,把他那段代码直接复制过来,再自定一个page ,就轻松搞定了 。记录下

public  class UcViewHelper<T> where T:UserControl
    {
       private MyPage m_pageHolder;
       public T LoadViewControl(string path)
       {
           m_pageHolder = new MyPage();
           return (T) m_pageHolder.LoadControl(path);
       }
       public string RenderView(T control)
       {
           StringWriter output = new StringWriter();
           
           this.m_pageHolder.Controls.Add(control);
           HttpContext.Current.Server.Execute(this.m_pageHolder, output, false);

           return output.ToString();
       }
    }

  class MyPage:Page
{
    public override void VerifyRenderingInServerForm(Control control)
    {
        
    }
}

$.ajax({
   type: "GET",
   url: "XXXXAjaxHandler.ashx",
   dataType: "html",
   data: { },
   success: function (data, textStatus) {
    $("#KeyValueList").html(data);
              },
   error: function (XMLHttpRequest, textStatus, errorThrown) {
       }
  })//AJAX-end

 

 UcViewHelper<ClassName> viewManager = new UcViewHelper<ClassName>();
  ClassName control = viewManager.LoadViewControl("XXXX.ascx");
  context.Response.Write(viewManager.RenderView(control));

posted on 2011-01-20 17:10  fuhui  阅读(383)  评论(0编辑  收藏  举报