HtmlContainerControl.InnerText 屬性

定義

取得或設定在指定的 HTML 伺服器控制項的開始和結尾標記之間的文字。Gets or sets the text between the opening and closing tags of the specified HTML server control.

public:
 virtual property System::String ^ InnerText { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.Browsable(false)]
public virtual string InnerText { get; set; }
member this.InnerText : string with get, set
Public Overridable Property InnerText As String

屬性值

介於 HTML 伺服器控制項的開始和結尾標記之間的文字。The text between the opening and closing tags of an HTML server control.

屬性

例外狀況

有一個以上的 HTML 伺服器控制項。There is more than one HTML server control.

-或--or- HTML 伺服器控制項不是 LiteralControlDataBoundLiteralControlThe HTML server control is not a LiteralControl or a DataBoundLiteralControl.

範例

下列程式碼範例示範可透過 InnerText 屬性取得的 HTML 編碼服務。The following code example demonstrates the HTML encoding service available through the InnerText property.

<%@ Page Language="C#"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  protected void Page_Load(Object Src, EventArgs E)
  {
    Message.InnerText = "To make text bold, use the <b> tag.";
    
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>HtmlContainerControl Example</title>
</head>
  <body>
    <form id="form1" runat="server">
    <div>
    <b><span id="Message" runat="server"></span></b>
    </div>
    </form>
  </body>
</html>    
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  Protected Sub Page_Load(ByVal Src As Object, ByVal E As EventArgs)
    
    Message.InnerText = "To make text bold, use the <b> tag."
    
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head2" runat="server">
    <title>HtmlContainerControl Example</title>
</head>
  <body>
    <form id="form1" runat="server">
    <div>
    <b><span id="Message" runat="server"></span></b>
    </div>
    </form>
  </body>
</html>      

備註

使用 [InnerText] 屬性,以程式設計方式修改 HTML 伺服器控制項開頭和結束記號之間的文字。Use the InnerText property to programmatically modify the text between the opening and closing tags of an HTML server control.

不同于 InnerHtml 屬性,InnerText 屬性會自動對 HTML 實體進行特殊字元的編碼。Unlike the InnerHtml property, the InnerText property automatically encodes special characters to and from HTML entities. HTML 實體可讓您顯示瀏覽器通常會解讀為具有特殊意義的特殊字元,例如 < 字元。HTML entities allow you to display special characters, such as the < character, that a browser would ordinarily interpret as having special meaning. < 字元會解讀為標記的開頭,而且不會顯示在頁面上。The < character would be interpreted as the start of a tag and is not displayed on the page. 若要顯示 < 字元,您必須使用實體 &lt;To display the < character, you would need to use the entity &lt;.

例如,如果 InnerText 屬性設為 "<b> Hello </b>",則 <> 符號會分別轉換成 &lt;&gt;For example, if the InnerText property is set to "<b> Hello </b>", the < and > symbols are converted to &lt; and &gt;, respectively. 呈現的輸出會是: < b > Hello </b >。The rendered output would be: < b > Hello < /b >. &lt;&gt; 實體會向瀏覽器指出這些字元要顯示在頁面上。The &lt; and &gt; entities would indicate to the browser that these characters are to be displayed on the page. 瀏覽器將不會偵測到 <b> 標記,並以粗體字顯示文字。The browser will not detect the <b> tags and display the text in a bold font. 頁面上顯示的文字為: <b > Hello </b >。The text displayed on the page is: <b> Hello </b>.

若要防止自動進行 HTML 編碼和解碼,請使用 InnerHtml 屬性。To prevent automatic HTML encoding and decoding, use the InnerHtml property.

注意

如果沒有子控制項,InnerHtml 屬性會包含 String.Empty的值。If there are no child controls, the InnerHtml property contains the value String.Empty.

適用於

另請參閱