2011-01-17 11 views
1

먼저이 분야의 초보자라는 사실을 튜토리얼을 통해 알려야합니다.버튼을 클릭 할 때 소스 코드 표시

그 말은 codebehind 파일에서 버튼을 클릭하면 텍스트 상자에 sourcecode를로드하는 방법을 찾고 있다고 말했습니다. 같은 aspx 파일에 대한 간다.

나는이 웹 사이트를 만들고 있는데, 여기서 내가하는 일의 코드 예제를 보여줄 것이다. 따라서 myweb.com/tutorial1done.aspx로 이동하면이 페이지는 나에게 자습서의 최종 결과를 표시합니다. 쇼 소스 단추를 클릭하면 2 개의 텍스트 상자가 보이게하고 첫 번째 상자에 codebehind를 추가하고 aspx 소스를 두 번째 상자에 추가해야합니다.

가능한지 모르겠지만 희망합니다. 쉬운이해야하지 않는 한, 지금 소스 강조가 필요하지 않은 경우

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; 
     } 

    } 

} 
} 

:

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.

+0

미리 정의 된 소스 코드를 의미하는지 또는 현재 페이지의 소스 코드를 언급하고 있는지 확실하지 않은가요? –

+0

나는 현재 페이지의 출처를 언급하고 있었다. - 그러나 mattens 대답은 가능하지 않다는 것을 알려준다. :) – fjappe

답변

1

코드 숨김 파일의 실제 코드를 참조하는 경우 문제가 있습니다. 파일을 컴파일 한 다음 동적 링크 라이브러리 (.dll)에 중간 코드로 저장하면 더 이상 .aspx.cs 파일에 액세스 할 수 없게됩니다. 이동할 수있는 유일한 방법은 deployd 프로젝트에 코드 숨김 파일을 포함시키고 FileStream (또는 무엇이든)을 열어 코드를 읽고 내용을 표시하는 것입니다.

+0

글쎄. .cs 파일을 .txt 파일에 복사 한 것과 같다. 하나? 그것은 어떻게 든 내 자신을 쉽게 만들어주는 것이 었습니다 - 그래서 완성 된 모든 튜토리얼에 사용할 수있는 기능을 가질 수있었습니다. – fjappe

+0

빌드 옵션에서 파일을 올바른 위치 (bin 디렉토리)로 자동 복사 할 수있는 빌드 후 스크립트를 지정할 수 있습니다. – Matten

+0

그게 나를 위해 약간의 타격을받는 것처럼 보입니다. 지금은 asp.net과 visual studio에서 완전히 녹색입니다. – fjappe

관련 문제