简体   繁体   English

动态创建带有CodeBehind的ASP.NET页

[英]Create ASP.NET page with CodeBehind Dynamically

I'd like to create a simple 我想创建一个简单的

default.aspx default.aspx.cs default.aspx default.aspx.cs

page dyamically and store it on the server. 动态地翻页并将其存储在服务器上。

I've used StreamWriter to create the directory and both files 我使用StreamWriter创建目录和两个文件

The default.aspx I create doesn't access the codebehind. 我创建的default.aspx无法访问后台代码。

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! 任何建议,谢谢!

您可以尝试使用母版页的代码隐藏,以避开事件,并仅动态创建内容页。

Why create a code-behind at all when you can just add that code in the aspx-file? 当您只需将代码添加到aspx文件中时,为什么还要创建一个代码隐藏? Much simpler and you still have all your page_load events and such 简单得多,您仍然拥有所有的page_load事件,例如

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

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