user1122321
user1122321

Reputation: 51

Compile aspx page "on the fly"

I am trying to build an aspx page at runtime (by another aspx page which finally redirects to the new one). As far as I understand, aspx pages MUST be precompiled before a user can view them. In other words, the aspx page must be compiled to the DLL in the /bin folder.

Is there a away to tell IIS, or to order it by VB.NET code, to compile a page before I am redirecting my user to the page?

Any help would be greatly appriciated.

Upvotes: 1

Views: 1161

Answers (2)

You can use the VirtualPathProvider class to load pages from a database.

Upvotes: 1

Maxim Kornilov
Maxim Kornilov

Reputation: 6075

Basicallly what you need is to render content of the page dynamically. You can create page content dynamically on server side by adding controls (HTML or Server Ones) to controls collection for example to place holder server element.

For example you can create page with the following markup:

    <%@ 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>

And then in code behind class we can add controls to render which is necessary dynamically be reading information from database.

    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
    }
}

Note: this example can be a start point to create dynamic pages which content depend on values specified in database or configuration.

Upvotes: 0

Related Questions