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?
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());
}
}
我不相信有任何方法可以自动生成标记,但是您可以创建自己的Render方法,以输出所需的基本HTML框架。
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.
如果您的“页面”是完全动态的并且没有aspx前端(我没有意识到这是可能的吗?!)……那么您真正想要的可能是自定义HttpHandler而不是从Page继承。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.