开发者

How to make an asp:panel render as a div via .RenderConrol

开发者 https://www.devze.com 2023-01-08 23:13 出处:网络
tl:dr version: Is there a way to get the panels to render as divs when rendered by RenderControl? Background:

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.


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.


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:

public class MyPanel : Panel
{
    //snip

    protected override void Render(HtmlTextWriter output)
    {
        output.Write("<div id=\"{0}\" class=\"{1}\"", this.ID, this.CssClass);
        this.RenderChildren(output);
        output.Write("</div>");
    }

    //snip
}

This also worked great.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号