在asp.net中创建动态.aspx页面

时间:2013-06-25 11:57:49

标签: c# asp.net

以下代码创建file.aspx和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();
}

我想将页面名称写在我的文本框中。

我尝试将文本框放在上面代码的html源代码中,但是我收到了错误。

CodeFile="""+TextBox1.Text+""" Inherits="""+TextBox1.Text+"""

2 个答案:

答案 0 :(得分:1)

以ASP.NET“思考”页面的方式工作将会更加遥远。我曾经做过一个非常大的动态调查问卷。所有控件都是动态生成的,还有验证和其他所有内容。在它的核心,我们这样做的方式是:

  • 在页面上放置一个面板
  • 向面板添加控件

代码非常粗略地看起来像这样:

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

因为您正在处理动态控件,您可能还需要确保您了解页面生命周期...:http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx

答案 1 :(得分:0)

确保您的网络项目被声明为web site rather than a web application

与Web应用程序不同,网站愿意按需动态编译每个页面,因此这样的事情原则上是可行的。如果你真的想这样做。