js获取UserControl内容,避免拼html的麻烦
时间:2011-01-20 来源:fuhui
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));