我想使用jquery ajax加載用戶控件。我發現的一個可能是通過通用處理程序加載用戶控件。任何人都可以幫我解決問題。這裏是我用來調用控件的ajax代碼。我們如何使用泛型處理函數加載usercontrol?
<script type="text/javascript">
function fillSigns() {
$.ajax({
url: "usercontrolhandler.ashx?control=signs.ascx",
context: document.body,
success: function (data) {
$('#signdiv').html(data);
}
});
}
</script>
,這裏是在處理程序文件
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
Page page = new Page();
UserControl ctrl = (UserControl)page.LoadControl("~/" + context.Request["control"] + ".ascx");
page.Form.Controls.Add(ctrl);
StringWriter stringWriter = new StringWriter();
HtmlTextWriter tw = new HtmlTextWriter(stringWriter);
ctrl.RenderControl(tw);
context.Response.Write(stringWriter.ToString());
}
代碼此代碼提高在低於所示的線未找到對象引用錯誤。
page.Form.Controls.Add(ctrl);
沒有必要創建一個'Page'對象以呈現'UserControl'。刪除該代碼,你會沒事的。 – 2011-03-31 12:24:24