Andre Walker
Andre Walker

Reputation: 79

Create ASP.NET page with CodeBehind Dynamically

I'd like to create a simple

default.aspx default.aspx.cs

page dyamically and store it on the server.

I've used StreamWriter to create the directory and both files

The default.aspx I create doesn't access the codebehind.

private string displayPage = @"<%@ Page Language=""C#"" AutoEventWireup=""true"" CodeBehind=""Default.aspx.cs""  %>

<!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"">
    <div>
        <asp:Label id=""_lblBody"" runat=""server"" />
        <asp:Label id=""_lblFooter"" runat=""server"" />
    </div>
    </form>
</body>
</html>";

        private string codeBehindPage = @"using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

    public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            _lblBody.Text = ""Hello World!"";
        }
    }
";

Can this be done? Any advice, thanks!

Upvotes: 0

Views: 1396

Answers (2)

Mattias &#197;slund
Mattias &#197;slund

Reputation: 3907

Why create a code-behind at all when you can just add that code in the aspx-file? Much simpler and you still have all your page_load events and such

Upvotes: 0

user1240641
user1240641

Reputation: 11

You can try to use masterpage's codebehind, to hadndle events, and dynamically create only content-pages.

Upvotes: 1

Related Questions