我们如何使用通用处理程序加载用户控件?
我想使用 jquery ajax 加载用户控件。我发现的一种可能是通过通用处理程序加载用户控件。任何人都可以帮助我。这是我用来调用控件的ajax代码。
<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);
i want to load a user control using jquery ajax. One possible i found is to load usercontrol through generic handler. Anyone help me plsss. here the ajax code i am using to call the control.
<script type="text/javascript">
function fillSigns() {
$.ajax({
url: "usercontrolhandler.ashx?control=signs.ascx",
context: document.body,
success: function (data) {
$('#signdiv').html(data);
}
});
}
</script>
and here is the code in handler file
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());
}
This code raises object reference not found error at below shown line.
page.Form.Controls.Add(ctrl);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来
page.Form
在这里是null
,这就是为什么你有一个空引用异常。您可以将用户控件添加到页面的控件集合中:您还可以使用
HttpServerUtility.Execute
页面渲染方法:最后看一下 提示/技巧:与 ASP.NET AJAX 一起使用的 Cool UI 模板技术Scott Guthrie 的非 UpdatePanel 场景 文章涵盖了您的问题。
It seems
page.Form
isnull
here, that's why you've got a null reference exception. You could add your user control to the page's control collection instead:You could also use
HttpServerUtility.Execute
method for page rendering:And finally take a look onto Tip/Trick: Cool UI Templating Technique to use with ASP.NET AJAX for non-UpdatePanel scenarios article by Scott Guthrie which covers your problem.
试试这个:
然后:
Try this:
then: