0

The following code creates file.aspx and file.aspx.cs:

protected void Button1_Click(object sender, EventArgs e)
{
    string fielName = Server.MapPath("~/file.aspx");        
    TextWriter tw = new StreamWriter(fielName);        
    tw.WriteLine(@"<%@ Page Language=""C#"" AutoEventWireup=""true""  CodeFile=""file.aspx.cs"" Inherits=""file"" %>
                   <!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>

                        </div>
                        </form>
                    </body>
                    </html>
                    ");        
    tw.Close();
    tw = new StreamWriter(fielName + ".cs");

    tw.WriteLine(@"using System;
                    using System.Collections.Generic;
                    using System.Linq;
                    using System.Web;
                    using System.Web.UI;
                    using System.Web.UI.WebControls;
                    using System.IO;

                    public partial class file : System.Web.UI.Page
                    {
                        protected void Page_Load(object sender, EventArgs e)
                        {
                           Response.Write(""new File "");

                        }
                    }
                    ");       
    tw.Close();
}

I want to make the page name written in my Textbox.

I have tried putting textbox in html source of above code, but I'm getting an error.

CodeFile="""+TextBox1.Text+""" Inherits="""+TextBox1.Text+"""
5
  • What you are doing is not a good practice instead use an html editor and save your contect in database and when you need your content load your page with page id .let me know if you need further help. Second there will be security issue on online server Commented Jun 25, 2013 at 12:01
  • How will your pages be dynamic if you just create a new aspx page (and codebehind) on the server folder structure? Depending on your deployment you will need to compile those pages. I'm pretty sure that aspx pages are already "dynamic" enough to fit your purposes, what ever it is you are trying to achieve. Commented Jun 25, 2013 at 12:05
  • Depends on the framework you are working with. 4.0 definitely requires compiling. The classes are actually loaded from a compiled assembly in the bin folder. I can't remember if you can even make the old web projects like this in 4.0. Maybe. I know I used to years ago have a web project on 2.0 that dynamically compiled the cs files. Commented Jun 25, 2013 at 12:07
  • 1
    My first question is "Why?" What overall goal are you trying to achieve by coding your aspx's like this? Commented Jun 25, 2013 at 12:13
  • @valverij I required Commented Jun 26, 2013 at 12:39

2 Answers 2

1

You would be much farther ahead to work the way ASP.NET "thinks" about the page. I once worked on a very large, dynamic questionnaire. All of the controls were generated dynamically along with validations and everything else. At it's core, the way we did it was:

  • place a panel on the page
  • add controls to the panel

the code very roughly would look something like this:

        var btn = new Button();
        btn.ID = "theId";
        btn.Text = "hi";
        pnlDynamic.Controls.Add(btn);

Because you're dealing with dynamic controls, you might also want to make sure that you understand the page life-cycle...: http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx

Sign up to request clarification or add additional context in comments.

Comments

0

Make sure your web project is declared as a web site rather than a web application.

A web site is willing to dynamically compile each page on demand, unlike a web application, so something like this is in principle doable. If you really want to do it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.