Asp.Net 前台顯示後台值

    可以使用多种方法从 ASP.NET 程序显示信息。一种方法是使用 <%= %> 结构。另一种方法是使用 Response.Write 语句。

1、使用 <%= %>
    从 ASP.NET 程序显示信息的最简单方法是使用 <%= %> 结构。在等号后面输入的值将写入当前页。
    1.1 格式為:
        <%= 後台變量%>
    1.2 例 :
        1.2.1 後台代碼:
public partial class Default_null : System.Web.UI.Page
{
    
/// <summary>
    
/// 定義在前台顯示的變量
    
/// </summary>
    public string sValue = "";
    
protected void Page_Load(object sender, EventArgs e)
    {

        
/*
          給變更量賦值
          注:@"",表示引號中內容全部都為一個字符串
         
*/
        sValue 
= @"這是一個要在前台顯示的內容,
                 在這裡我們有多行進行測試
";
    }
}

        1.2.2 前台源碼:
<head runat="server">
    
<title>未命名頁面</title>
</head>
<body>
    
<form id="form1" runat="server">
        
<div>
            
<!--sValue為後台定義的變量,此變量值將會在此處顯示-->
            
<%=sValue %>
        
</div>
    
</form>
</body>
</html>

        1.2.3 結果為:
這是一個要在前台顯示的內容, 在這裡我們有多行進行測試

2、Response.Write 语句
    显示文本的另一种方法是使用 Response.Write 语句。可以将它放在 <% %> 块内。
    2.1 格式
        <% Response.Write("需顯示的內容") %> 。
       Response.Write 语句的输出被合并到正在处理的页中。这样就允许 Response.Write 的输出编写代码,而该代码又可显示文本。
    2.2 例 :
        2.2.1前台源碼中寫入:
<head runat="server">
    
<title>未命名頁面</title>
</head>
<body>
    
<form id="form1" runat="server">
        
<div>
        
<!--在此處顯示內容-->
        
<%Response.Write("<br>Response.Write測試"); %>
        
</div>
    
</form>
</body>
</html>

        2.2.2 結果:
Response.Write測試




原文地址:https://www.cnblogs.com/scottckt/p/1242932.html