如何通过 .RenderConrol 将 asp:panel 渲染为 div
tl:dr 版本:
有没有办法让面板在由 RenderControl 渲染时渲染为 div?
背景:
我正在一个 asp.net 1.1 站点上工作,我正在尝试将包含面板的控件呈现为字符串,以便我可以将其作为 JSON 传递回页面并刷新页面的一部分。
我这样渲染该控件:
StringWriter twHeader = new StringWriter();
Html32TextWriter HeaderWriter = new Html32TextWriter(twHeader);
MyHeader.RenderControl(HeaderWriter);
HeaderWriter.Close();
string HeaderHtml = twHeader.ToString()
当导航到包含 MyHeader 控件的页面时,该控件内的面板将呈现为 div。但是,当我在 MyHeader 上调用 RenderControl 时,面板将呈现为表格,因此当使用 HeaderHtml 更新页面时,布局会爆炸。
有没有办法让面板在由 RenderControl 渲染时渲染为 div?
附加信息:
当我第一次研究这个问题时,我认为这可能是 browsercaps 问题,但是当我查看 web.config 时,我发现我们已经按照此站点设置了 browsercaps: http://sling Five.com/pages/code/browserCaps/
我想我可以通过使用 < 来解决这个问题;div runat="server">
而不是面板,最终这可能是最好的解决方案,但此时我很好奇是否有办法让面板执行我想要的操作它到。
tl:dr version:
Is there a way to get the panels to render as divs when rendered by RenderControl?
Background:
I am working on an asp.net 1.1 site, and I am trying to render a control containing panels to a string so I can pass it back to the page as JSON and refresh part of the page.
I am rendering the control thusly:
StringWriter twHeader = new StringWriter();
Html32TextWriter HeaderWriter = new Html32TextWriter(twHeader);
MyHeader.RenderControl(HeaderWriter);
HeaderWriter.Close();
string HeaderHtml = twHeader.ToString()
When navigating to a page containing a MyHeader control, the panels within that control are rendered as divs. However, when I call RenderControl on MyHeader, the panels are rendered as tables, so when the page is updated with HeaderHtml, the layout blows up.
Is there a way to get the panels to render as divs when rendered by RenderControl?
Additional Information:
When I first looked into the problem I thought it might be a browsercaps issue, but when I looked at the web.config I saw that we already have browsercaps set as per this site: http://slingfive.com/pages/code/browserCaps/
I think I could get around this problem by using <div runat="server">
instead of panels, and in the end this might be the best solution, but at this point I am very curious if there is a way to get the panel to do what I want it to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我倾向于做的是创建一个继承自Panel(例如MyPanel)的类,使用它代替Panel 并重写MyPanel 中的Render 方法来执行我想要的操作。
What I tend to do is create a class that inherits from Panel (MyPanel, say), use that in place of the Panel and override the Render method in MyPanel to do what I want.
在看到 Yellowfog 的答案之前,我继续尝试使用服务器端 div 而不是面板。讽刺的是,我实际上正在使用一个继承自Panel的类,并且它恰好被称为MyPanel:不确定这是否为Yellowfog赢得了通灵徽章,或者令人毛骨悚然的徽章:)。我不想采用这种方法的主要原因是因为我没有编写 MyPanel 类,并且不想处理由于从 HtmlGenericControl 继承而可能必须对其进行的任何更改面板。但这种切换非常轻松,而且效果很好。
读完黄雾的回答后,我想我也应该尝试一下他的建议:
这也很有效。
Before I saw Yellowfog's answer, I went ahead and tried using server side divs instead of Panels. Ironically, I actually am using a class that inherits from Panel, and it happens to be called MyPanel: not sure if that earns Yellowfog a psychic badge, or a creepy badge :) . The main reason I didn't want to take this approach is because I didn't write the MyPanel class, and didn't want to have to deal with any changes to it I might have to make as a result of inheriting from HtmlGenericControl instead of Panel. But the switch was pretty painless, and it worked great.
After reading Yellowfog's answer, I thought I'd give his suggestion a whirl as well:
This also worked great.