【问题标题】:how can i create a dynamic page in asp.net (C#)?如何在 asp.net (C#) 中创建动态页面?
【发布时间】:2011-06-01 08:43:53
【问题描述】:

我在我的网站应用程序中添加了一个页面“First.aspx”。在 First.aspx 页面中,我有一个名为 btnbutton 的按钮。 “btnbutton”的“onclick”事件应该打开一个新的动态页面。 我怎样才能做到这一点。? 请记住,新创建的动态页面不存在于应用程序中。该页面应该在运行时创建并且也是动态的。 请帮帮我!

【问题讨论】:

  • 据我所知,没有什么比你要问的“动态创建的 ASP.NET 页面”...
  • Akram Shahda:我不这么认为
  • 如果你能用简单的英语(没有编程术语)解释你想要达到的目标会更好。
  • @eugeneK: 我想在运行时打开新页面,在那个页面上我想要一个文本框和一个按钮
  • @amitvyas100688:要打开 ASP.NET 页面,必须编译后面的代码。你将如何编译生成页面的代码??

标签: c# asp.net


【解决方案1】:

如果您询问的是在运行时 生成ASP.NET 页面,那是不可能的。原因如下:

你需要编译的代码 ASP.NET 运行前的页面。然后 在你的网络之后是不可能的 应用程序已启动。

但是,如果您询问页面之间的导航,则可以使用Response.Redirect

Response.Redirect("http://www.stackoverflow.com/");

【讨论】:

  • 我知道 Navigation bro,但我想在我的 Web 应用程序启动后创建一个新页面。 :(
【解决方案2】:

您可以在 ASP.net 网站中创建新的动态文件(而不是在 ASP.net Web 应用程序中)。 但有一个问题 。创建文件后,整个网站将重新启动以编译新创建的文件。所以你的 Session 数据会丢失。

这是创建新文件的代码。

    string fielName = Server.MapPath("~/file.aspx");
        //File.Create(fielName);
        //File.AppendText(fielName);


        // create a writer and open the file
        TextWriter tw = new StreamWriter(fielName);

        // write a line of text to the file
        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>
");

        // close the stream
        tw.Close();


        tw = new StreamWriter(fielName + ".cs");

        // write a line of text to the file
        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 "");

    }
}
");

        // close the stream
        tw.Close();

【讨论】:

  • 假设我必须在我的文本框中给出我的页面的动态名称,并且还要更改 html 页面中的 CodeFile="" Inherits="" 的名称我该怎么办?我试过 CodeFile="""+Textbox1.Text+""" Inherits="""+Textbox1.Text+""" 但显示错误
  • 您遇到了什么错误..?您还必须在此处更改名称.. Server.MapPath("~/file.aspx")
  • 我得到的错误是:- 编译器错误消息:CS1010:常量中的换行 // 将一行文本写入文件 tw.WriteLine(@" //W3C// DTD XHTML 1.0 过渡//EN"" ""w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
  • 这只是格式化文字字符串的问题。试试这个代码tw.WriteLine(@"&lt;%@ Page Language=""C#"" AutoEventWireup=""true"" CodeFile=""" + TextBox1.Text + @".cs"" Inherits=""" + TextBox1.Text + @"""
猜你喜欢
  • 1970-01-01
  • 2012-05-11
  • 1970-01-01
  • 1970-01-01
  • 2017-01-03
  • 1970-01-01
  • 2021-12-17
  • 1970-01-01
  • 2012-03-25
相关资源
最近更新 更多