让脚本管理器进入动态呈现的页面

发布于 2024-08-29 05:45:48 字数 659 浏览 4 评论 0原文

我们像这样动态渲染用户控件:

public string RenderControl(string pathcontrol)
{
    string html;

    var page = new Page();
    var control = page.LoadControl(path);            
    page.Controls.Add(control);

    // do stuff to the control (give it some data to work on)

    using (var writer = new StringWriter())
    {
        HttpContext.Current.Server.Execute(page, writer, false);
        html = writer.ToString();
    }

    return html;
}

这让我们在正常渲染页面时使用与渲染 ajax 调用响应时相同的用户控件。但是,当添加本身包含 scriptmanagerProxy 的控件时,我们会遇到这样的问题:新的 Page 对象不包含 ScriptManager 或 ScriptManager 需要在其中运行的 HtmlForm。

有什么办法解决这个问题吗?

你的 安德烈亚斯

We are rendering usercontrols dynamically like this:

public string RenderControl(string pathcontrol)
{
    string html;

    var page = new Page();
    var control = page.LoadControl(path);            
    page.Controls.Add(control);

    // do stuff to the control (give it some data to work on)

    using (var writer = new StringWriter())
    {
        HttpContext.Current.Server.Execute(page, writer, false);
        html = writer.ToString();
    }

    return html;
}

This lets us the same user controls when rendering pages normally as we do when rendering responses to ajax calls. However, when adding controls which themselves contain a scriptmanagerProxy we run into the problem that the newed up Page object doesn't contain either a ScriptManager or the HtmlForm in which the ScriptManager needs to run.

Is there any way around this?

Yours
Andreas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

樱花坊 2024-09-05 05:45:48

正如其他人所说,您可以足够轻松地动态添加 ScriptManger [ 添加 ScriptManager以编程方式分页?(如果您的Page 对象已完成)。

您可以尝试使用 BuildManager.CreateInstanceFromVirtualPath()< /a> 来创建 Page 对象?您的问题可能是如何创建该对象。创建新页面比更新 Page 对象要复杂一些。

例如。

Page page 
       = BuildManager.CreateInstanceFromVirtualPath("~/Test.aspx", typeof(Page))

另请参阅http://www.west-wind.com/weblog/posts/ 120530.aspx 了解更多背景信息。

As others have said you can add a ScriptManger dynamically easily enough [ Add ScriptManager to Page Programmatically? if your Page object is complete.

Can you try using BuildManager.CreateInstanceFromVirtualPath() to create the Page object instead? You issue may be how you create that object. There's a bit more to creating a new page than newing up the Page object.

Eg.

Page page 
       = BuildManager.CreateInstanceFromVirtualPath("~/Test.aspx", typeof(Page))

See also http://www.west-wind.com/weblog/posts/120530.aspx for a little more background.

哑剧 2024-09-05 05:45:48

你能做这样的事情:

page.Form.Controls.AddAt(0, New ScriptManager())

编辑:我认为你还需要将控件添加到页面的表单中,而不仅仅是页面本身,对吧?据我了解,表单是随页面创建的,但如果不是,您应该能够执行以下操作:

page.Form = new HtmlForm()

您可能还需要执行以下操作:

page.Controls.Add(page.Form)

Can you do something like this:

page.Form.Controls.AddAt(0, New ScriptManager())

Edit: I think you'd also need to add your control to the page's form, not just to the page itself, right? It's my understanding that the form is created with the page, but if not you should be able to just do:

page.Form = new HtmlForm()

You may also need to do something like:

page.Controls.Add(page.Form)
早乙女 2024-09-05 05:45:48

当然,诀窍是将其添加到页面的 Init 事件处理程序中。您可以使用:

Page.Init += delegate {

  // check for script manager
  if( ScriptManager.GetCurrent(Page) == null ) {

    ScriptManager m = new ScriptManager();
    m.ScriptMode = ScriptMode.Release;
    Page.Form.Controls.AddAt(0, m);

  }

}

如果可以的话,我建议避免动态添加表单到您的页面。例如,上面的代码片段假设页面上已经存在表单。

更新

当然,感谢您指出安德烈亚斯。这是一个更新。因此,Page.Form 没有设置器 - 但您是正确的,因为您可以将新的 HtmlForm 添加到 Controls 集合中。添加后,Page.Form 属性不再为空。这将允许您动态添加 ScriptManager,如上所示。下面是一个代码示例,显示了此工作原理(ASPX 文件是一个简单的页面,没有服务器端表单):

public partial class Pages_Test_DynamicFormSample : Page {

    protected void Page_Init(object sender, EventArgs e) {

        Controls.Add( new HtmlForm() );

        ScriptManager m = new ScriptManager();
        m.ScriptMode = ScriptMode.Release;
        Form.Controls.AddAt(0, m);

    }

    protected void Page_Load(object sender, EventArgs e) {

        // ScriptManager test
        var t1 = new System.Web.UI.WebControls.TextBox();
        var t2 = new System.Web.UI.WebControls.TextBox();
        Form.Controls.Add( t1 );
        Form.Controls.Add( t2 );

        ScriptManager.GetCurrent(Page).SetFocus( t2 );

    }

}

享受 - 顺便说一句,将 ScriptManager 的 ScriptMode 设置为 Release 显然不是必需的。我们这样做只是为了避免在 ASP.NET 脚本运行时的调试版本中发现一些 JavaScript 错误。

Sure, the trick is to add it in a page's Init event handler. You can use:

Page.Init += delegate {

  // check for script manager
  if( ScriptManager.GetCurrent(Page) == null ) {

    ScriptManager m = new ScriptManager();
    m.ScriptMode = ScriptMode.Release;
    Page.Form.Controls.AddAt(0, m);

  }

}

I'd recommend avoiding dynamically adding forms to your page if you can. For example, the above code snippet assumes a form is already present on the page.

Update

Sure, thanks for pointing that out Andreas. Here's an update. So, there is no setter for Page.Form - but you are correct in that you can add a new HtmlForm to the Controls collection. Once added, the Page.Form property is no longer null. That will allow you to add the ScriptManager dynamically as seen above. Here is a code sample that shows this working (ASPX file is a simple page without a server side form):

public partial class Pages_Test_DynamicFormSample : Page {

    protected void Page_Init(object sender, EventArgs e) {

        Controls.Add( new HtmlForm() );

        ScriptManager m = new ScriptManager();
        m.ScriptMode = ScriptMode.Release;
        Form.Controls.AddAt(0, m);

    }

    protected void Page_Load(object sender, EventArgs e) {

        // ScriptManager test
        var t1 = new System.Web.UI.WebControls.TextBox();
        var t2 = new System.Web.UI.WebControls.TextBox();
        Form.Controls.Add( t1 );
        Form.Controls.Add( t2 );

        ScriptManager.GetCurrent(Page).SetFocus( t2 );

    }

}

Enjoy - btw, setting the ScriptManager's ScriptMode to Release obviously isn't required. We do it just to avoid some JavaScript bugs found in the Debug version of the ASP.NET script runtime.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文