C# 单击按钮时显示源代码

C# 单击按钮时显示源代码,c#,asp.net,C#,Asp.net,首先,我必须让你知道我是这个领域的新手,从教程中学习 说到这里,我正在寻找一种方法,当单击按钮时,将源代码从codebehind文件加载到文本框中。aspx文件也是如此 我正在制作这个网站,在这里我将展示我所做的代码示例。因此,如果我导航到myweb.com/tutorial1done.aspx,该页面将显示完成的教程的最终结果。当我单击ShowSource按钮时,它应该使两个文本框可见,并将codebehind添加到第一个框中,将aspx源添加到第二个框中 我不知道这是否可能,但我希望如此 到

首先,我必须让你知道我是这个领域的新手,从教程中学习

说到这里,我正在寻找一种方法,当单击按钮时,将源代码从codebehind文件加载到文本框中。aspx文件也是如此

我正在制作这个网站,在这里我将展示我所做的代码示例。因此,如果我导航到myweb.com/tutorial1done.aspx,该页面将显示完成的教程的最终结果。当我单击ShowSource按钮时,它应该使两个文本框可见,并将codebehind添加到第一个框中,将aspx源添加到第二个框中

我不知道这是否可能,但我希望如此

到目前为止,我有:

ASPX:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DateTimeOutput.aspx.cs" Inherits="WebApplication1.DateTimeOutput" %>

<!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>
<link rel="stylesheet" href="../styles/codeformatter.css" />
</head>
<body>
<form id="form1" runat="server">
<customControls:Header runat="server" heading="Date and Time Output" />
<div>
    <asp:Panel ID="Panel1" runat="server">
        <asp:TextBox ID="outputText" runat="server" Height="175px" TextMode="MultiLine" 
            Width="400px"></asp:TextBox>
    </asp:Panel>
</div>
<asp:Panel ID="Panel2" runat="server">
    <asp:Button ID="runButton" runat="server" Text="Run Code" 
        onclick="runButton_Click" Width="95px" />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Button ID="clearButton" runat="server" Text="Clear Console" 
        onclick="clearButton_Click" Width="95px" />
    <br />
    <br />
    <asp:Button ID="dt_showSource_btn" runat="server" 
        onclick="dt_showSource_btn_Click" Text="Show Source" />
</asp:Panel>
<asp:Label ID="dtLabel1" runat="server" Text="Code Behind:" Visible="False"></asp:Label>
<br />
<asp:TextBox ID="dtcb_output" runat="server" Height="175px" 
    TextMode="MultiLine" Visible="False" Width="400px"></asp:TextBox>
<br />
<br />
<asp:Label ID="dtLabel2" runat="server" Text="ASPX:" Visible="False"></asp:Label>
<br />
<asp:TextBox ID="dtaspx_output" runat="server" Height="175px" 
    TextMode="MultiLine" Visible="False" Width="400px"></asp:TextBox>
</form>
</body>
</html>
目前不需要源代码高亮显示,除非它很容易做到


Thx。如果您引用的是代码隐藏文件的实际代码,则会出现问题。由于该文件将被编译,然后作为中间代码放置在动态链接库(.dll)中,因此您不再有权访问.aspx.cs文件。唯一的方法是将代码隐藏文件包含在已部署的项目中,并使用FileStream(或其他)打开它以读取它并显示其内容。

如果您引用的是代码隐藏文件的实际代码,则会出现问题。由于该文件将被编译,然后作为中间代码放置在动态链接库(.dll)中,因此您不再有权访问.aspx.cs文件。唯一的方法是将代码隐藏文件包含在已部署的项目中,并使用FileStream(或其他)打开它阅读并显示其内容。

不确定您是指一些预定义的源代码还是指当前页面的源代码?我指的是当前页面的源代码,但mattens回答告诉我这是不可能的:)不确定您是指一些预定义的源代码还是指源代码对于当前页面?我指的是当前页面的来源-但mattens的回答告诉我这是不可能的:)好吧-我想这与我将.cs文件复制到.txt文件并读取该文件的情况相同?这是为了让我自己变得更容易-这样我就可以有一个函数用于我所有完成的教程。在构建选项中的某个地方,你可以指定一个构建后脚本,它可以自动将文件复制到正确的位置(到bin dir)。这对我来说似乎有点太复杂了,现在-我对asp.net和visual studio完全没有兴趣。没有什么大线索。。。它类似于
cp$(ProjectDir)/folder/file.aspx.cs$(TargetDir)/file.aspx.cs
(请参阅以获取更多帮助)!嗯,我想这和我将.cs文件复制到.txt文件并读取该文件是一样的?这是为了让我自己变得更容易-这样我就可以有一个函数用于我所有完成的教程。在构建选项中的某个地方,你可以指定一个构建后脚本,它可以自动将文件复制到正确的位置(到bin dir)。这对我来说似乎有点太复杂了,现在-我对asp.net和visual studio完全没有兴趣。没有什么大线索。。。它类似于
cp$(ProjectDir)/folder/file.aspx.cs$(TargetDir)/file.aspx.cs
(请参阅以获取更多帮助)!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class DateTimeOutput : System.Web.UI.Page
    {
    protected void output(String value)
    {
        outputText.Text += value + Environment.NewLine;
    }

    protected void runButton_Click(object sender, EventArgs e)
    {
        DateTime dt = new DateTime();
        output(dt.ToString());
        DateTime nowDt = DateTime.Now;
        output(nowDt.ToString());
    }

    protected void clearButton_Click(object sender, EventArgs e)
    {
        outputText.Text = "";
    }

    protected void dt_showSource_btn_Click(object sender, EventArgs e)
    {
        if (dtcb_output.Visible == false)
        {
            dtLabel1.Visible = true;
            dtcb_output.Visible = true;
        }
        else
        {
            dtLabel1.Visible = false;
            dtcb_output.Visible = false;
        }

        if (dtaspx_output.Visible == false)
        {
            dtLabel2.Visible = true;
            dtaspx_output.Visible = true;
        }
        else
        {
            dtLabel2.Visible = false;
            dtaspx_output.Visible = false;
        }

    }

}
}