动态生成的 ASP.NET 页面没有基本的 html 结构

发布于 2024-08-08 13:23:29 字数 368 浏览 5 评论 0原文

我编写了一个页面(仅从 System.Web.UI.Page 派生的类,我想在更多网站中使用它。我动态地将 webcontrol 添加到 Page.Controls 集合,如下所示:

Panel p = new Panel();
p.Style.Add("float", "left");
p.Controls.Add(locLT);
Page.Controls.Add(p);

此代码仅呈现

<div style="float:left;">
</div>

如何在不手动编写的情况下添加 HTML、HEADER 和 BODY 部分?这可能吗?

i wrote a page (only class that derives from System.Web.UI.Page, that i want to use in more websites. I dynamically add webcontrols to Page.Controls collection like this:

Panel p = new Panel();
p.Style.Add("float", "left");
p.Controls.Add(locLT);
Page.Controls.Add(p);

this code renders only

<div style="float:left;">
</div>

How can i add HTML, HEADER and BODY section without manually write this? Is it possible?

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

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

发布评论

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

评论(4

-残月青衣踏尘吟 2024-08-15 13:23:29

我推荐 MasterPages,但你可以这样做:

public class CustomBase : System.Web.UI.Page  
{    
    protected override void Render( System.Web.UI.HtmlTextWriter textWriter ) 
    {
        using (System.IO.StringWriter stringWriter = new System.IO.StringWriter())
        {
             using (HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter))
             {    
                 LiteralControl header =new LiteralControl();
                 header.Text = RenderHeader(); // implement HTML HEAD BODY...etc

                 LiteralControl footer = new LiteralControl();
                 footer.Text = RenderFooter(); // implement CLOSE BODY HTML...etc

                 this.Controls.AddAt(0, header); //top
                 this.Controls.Add(footer); //bottom

                 base.Render(htmlWriter); 
             }
        }

        textWriter.Write(stringWriter.ToString());
    }       

}

I recommend MasterPages but you can do this:

public class CustomBase : System.Web.UI.Page  
{    
    protected override void Render( System.Web.UI.HtmlTextWriter textWriter ) 
    {
        using (System.IO.StringWriter stringWriter = new System.IO.StringWriter())
        {
             using (HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter))
             {    
                 LiteralControl header =new LiteralControl();
                 header.Text = RenderHeader(); // implement HTML HEAD BODY...etc

                 LiteralControl footer = new LiteralControl();
                 footer.Text = RenderFooter(); // implement CLOSE BODY HTML...etc

                 this.Controls.AddAt(0, header); //top
                 this.Controls.Add(footer); //bottom

                 base.Render(htmlWriter); 
             }
        }

        textWriter.Write(stringWriter.ToString());
    }       

}
不乱于心 2024-08-15 13:23:29

我不相信有任何方法可以自动生成标签,但您可以创建自己的 Render 方法来输出所需的基本 HTML 框架。

I don't believe there is any way to automatically generate the tags, but you can create your own Render method that outputs the required basic HTML framework.

昇り龍 2024-08-15 13:23:29

在使用 MasterPages 之前,为页面添加页眉和页脚的常见方法是从 BasePage 继承,例如 http://gist .github.com/214437 并从 BasePage 加载页眉和页脚控件。我认为 MasterPage 比上面的 BasePage 更适合您。 MasterPage 的缺点之一是您必须在每个页面添加 asp:content,但它仍然比旧方法更好。

Before using MasterPages a common way to add header and footer for a page was to inherit from a BasePage like http://gist.github.com/214437 and from the BasePage load header and footer controls. I think that a MasterPage is better choice for you than the BasePage above. One of the drawbacks with a MasterPage is that you have to add asp:content at every page but it's still better than the old way.

回忆躺在深渊里 2024-08-15 13:23:29

如果你的“页面”是完全动态的并且没有 aspx 前端(我没有意识到这是可能的?!)...那么你真正想要的可能是一个自定义的 HttpHandler 而不是从 Page 继承。

If your "page" is completely dynamic and has no aspx front-end (I didn't realize this was possible?!)... then what you really want is probably a custom HttpHandler rather than inheriting from Page.

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