基本的に必要なのは、ページのコンテンツを動的にレンダリングすることです。コントロール (HTML または Server Ones) をコントロール コレクションに追加することで、サーバー側でページ コンテンツを動的に作成できます。たとえば、ホルダー サーバー要素を配置できます。
たとえば、次のマークアップを使用してページを作成できます。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestPage.aspx.cs" Inherits="StackOverflowWebApp.TestPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" />
<asp:PlaceHolder runat="server" ID="ContentPlaceHolder"></asp:PlaceHolder>
</form>
</body>
</html>
そして、コード ビハインド クラスでは、データベースから情報を動的に読み取るために必要な、レンダリングするコントロールを追加できます。
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace StackOverflowWebApp
{
public partial class TestPage : Page
{
#region Methods
protected override void CreateChildControls()
{
base.CreateChildControls();
// HERE get configuration from database.
// HERE create content of the page dynamically.
// Add reference to css file.
HtmlLink link = new HtmlLink { Href = "~/Styles/styles.css" };
link.Attributes.Add("type", "text/css");
link.Attributes.Add("rel", "stylesheet");
this.Page.Header.Controls.Add(link);
// Add inline styles.
HtmlGenericControl inlineStyle = new HtmlGenericControl("style");
inlineStyle.InnerText = "hr {color:sienna;} p {margin-left:20px;}";
this.Page.Header.Controls.Add(inlineStyle);
// Add div with css class and styles.
HtmlGenericControl div = new HtmlGenericControl("div");
this.ContentPlaceHolder.Controls.Add(div);
div.Attributes.Add("class", "SomeCssClassName");
div.Attributes.CssStyle.Add(HtmlTextWriterStyle.ZIndex, "1000");
TextBox textBox = new TextBox { ID = "TestTextBox" };
div.Controls.Add(textBox);
// and etc
}
#endregion
}
}
注: この例は、データベースまたは構成で指定された値にコンテンツが依存する動的ページを作成するための出発点となります。