我们如何使用通用处理程序加载用户控件?

发布于 2024-10-28 02:45:58 字数 1144 浏览 3 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事和酒 2024-11-04 02:45:58

看起来 page.Form 在这里是 null ,这就是为什么你有一个空引用异常。您可以将用户控件添加到页面的控件集合中:

page.Controls.Add(ctrl);

您还可以使用 HttpServerUtility.Execute 页面渲染方法:

StringWriter output = new StringWriter();
HttpContext.Current.Server.Execute(page, output, false);

最后看一下 提示/技巧:与 ASP.NET AJAX 一起使用的 Cool UI 模板技术Scott Guthrie 的非 UpdatePanel 场景 文章涵盖了您的问题。

It seems page.Form is null here, that's why you've got a null reference exception. You could add your user control to the page's control collection instead:

page.Controls.Add(ctrl);

You could also use HttpServerUtility.Execute method for page rendering:

StringWriter output = new StringWriter();
HttpContext.Current.Server.Execute(page, output, false);

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.

围归者 2024-11-04 02:45:58

试试这个:

Page page = new Page {ViewStateMode = ViewStateMode.Disabled};
HtmlForm form = new HtmlForm { ViewStateMode = ViewStateMode.Disabled };
form.Controls.Add(ctrl);
page.Controls.Add(form);

然后:

StringWriter stringWriter = new StringWriter();
HtmlTextWriter tw = new HtmlTextWriter(stringWriter);
page.RenderControl(tw);
context.Response.Write(stringWriter.ToString());

Try this:

Page page = new Page {ViewStateMode = ViewStateMode.Disabled};
HtmlForm form = new HtmlForm { ViewStateMode = ViewStateMode.Disabled };
form.Controls.Add(ctrl);
page.Controls.Add(form);

then:

StringWriter stringWriter = new StringWriter();
HtmlTextWriter tw = new HtmlTextWriter(stringWriter);
page.RenderControl(tw);
context.Response.Write(stringWriter.ToString());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文