让脚本管理器进入动态呈现的页面
我们像这样动态渲染用户控件:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如其他人所说,您可以足够轻松地动态添加 ScriptManger [ 添加 ScriptManager以编程方式分页?(如果您的Page 对象已完成)。
您可以尝试使用 BuildManager.CreateInstanceFromVirtualPath()< /a> 来创建 Page 对象?您的问题可能是如何创建该对象。创建新页面比更新 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.
See also http://www.west-wind.com/weblog/posts/120530.aspx for a little more background.
你能做这样的事情:
编辑:我认为你还需要将控件添加到页面的表单中,而不仅仅是页面本身,对吧?据我了解,表单是随页面创建的,但如果不是,您应该能够执行以下操作:
您可能还需要执行以下操作:
Can you do something like this:
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:
You may also need to do something like:
当然,诀窍是将其添加到页面的 Init 事件处理程序中。您可以使用:
如果可以的话,我建议避免动态添加表单到您的页面。例如,上面的代码片段假设页面上已经存在表单。
更新
当然,感谢您指出安德烈亚斯。这是一个更新。因此,Page.Form 没有设置器 - 但您是正确的,因为您可以将新的 HtmlForm 添加到 Controls 集合中。添加后,Page.Form 属性不再为空。这将允许您动态添加 ScriptManager,如上所示。下面是一个代码示例,显示了此工作原理(ASPX 文件是一个简单的页面,没有服务器端表单):
享受 - 顺便说一句,将 ScriptManager 的 ScriptMode 设置为 Release 显然不是必需的。我们这样做只是为了避免在 ASP.NET 脚本运行时的调试版本中发现一些 JavaScript 错误。
Sure, the trick is to add it in a page's Init event handler. You can use:
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):
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.