Επεξεργασία

Κοινή χρήση μέσω


HtmlContainerControl.InnerHtml Property

Definition

Gets or sets the content found between the opening and closing tags of the specified HTML server control.

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

Property Value

The HTML content between opening and closing tags of an HTML server control.

Attributes

Exceptions

There is more than one HTML server control.

-or-

The HTML server control is not a LiteralControl or a DataBoundLiteralControl.

Examples

The following code example demonstrates how to use the InnerHtml property to dynamically set a text message.

<%@ 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 sender, EventArgs e)
  {
    Message.InnerHtml = Server.HtmlEncode("Welcome! You accessed this page at: " + DateTime.Now);
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>HtmlContainerControl Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <span id="Message" runat="server"></span>    
    </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 sender As Object, ByVal e As System.EventArgs)

    Message.InnerHtml = Server.HtmlEncode("Welcome! You accessed this page at: " & DateTime.Now)

  End Sub

</script>

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

Remarks

Use the InnerHtml property to programmatically modify the contents between the opening and closing tags of an HTML server control.

The InnerHtml property does not automatically encode special characters to and from HTML entities. 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. To display the < character, you would need to use the entity &lt;.

For example, if the InnerHtml property is set to "<b> Hello </b>", the < and > characters are not converted to &lt; and &gt;, respectively. The rendered output would still be: <b> Hello </b>. The browser will detect the <b> tags and display the text, Hello, in a bold font.

Caution

Because the text is not HTML encoded, it possible to embed script within HTML tags in the text. If this property is set dynamically using user input, be sure to validate the value to reduce security vulnerabilities. For more information, see Security and User Input.

To provide automatic HTML encoding and decoding, use the InnerText property.

Note

If there are no child controls, the InnerHtml property contains the value String.Empty.

Applies to

See also