简体   繁体   English

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

[英]Create dynamic .aspx pages in asp.net

The following code creates file.aspx and file.aspx.cs: 以下代码创建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();
}

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. 我尝试将文本框放在上述代码的html源中,但出现错误。

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

You would be much farther ahead to work the way ASP.NET "thinks" about the page. 您将以ASP.NET“思考”页面的方式走得更远。 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 因为您正在处理动态控件,所以您可能还需要确保您了解页面的生命周期...: http : //msdn.microsoft.com/zh-cn/library/ms178472(v=vs。 100).aspx

Make sure your web project is declared as a web site rather than a web application . 确保将您的Web项目声明为网站而不是Web应用程序

A web site is willing to dynamically compile each page on demand, unlike a web application, so something like this is in principle doable. 与Web应用程序不同,网站愿意按需动态编译每个页面,因此原则上这样的事情是可行的。 If you really want to do it. 如果您真的要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM