TemplateControl.ParseControl 方法

定義

在 ASP.NET 網頁或使用者控制項上,將輸入字串剖析為 Control 物件。

多載

ParseControl(String)

在 Web Form 網頁或使用者控制項上,將輸入字串剖析為 Control 物件。

ParseControl(String, Boolean)

在 ASP.NET 網頁或使用者控制項上,將輸入字串剖析為 Control 物件。

備註

ParseControl.NET Framework 3.5 版引進。 如需詳細資訊,請參閱版本和相依性

ParseControl(String)

在 Web Form 網頁或使用者控制項上,將輸入字串剖析為 Control 物件。

C#
public System.Web.UI.Control ParseControl (string content);

參數

content
String

字串,包含使用者控制項。

傳回

剖析的 Control

範例

下列程式碼範例示範如何使用 方法來建立 Button Web 服務器控制項 ParseControl ,然後將結果新增至 ControlCollection 名為 myPlaceholderPlaceHolder Web 服務器控制項集合。

ASP.NET (C#)
<%@ Page language="c#" Debug="true" %>
<!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>
    <title>ASP.NET Example</title>
<script runat="server">
    // System.Web.UI.TemplateControl.ParserControl;
    // The following example demonstrates the method 'ParseControl' of class TemplateControl.
    // Since TemplateControl is abstract, this sample has been written using 'Page' class which derives from 
    // 'TemplateControl' class.
    // A button object is created by passing a string to contstruct a button using ASP syntax, to the 
    // 'ParseControl' method. This button is added as one of the child controls of the page and displayed.
    void Page_Load(object sender, System.EventArgs e)
    {
      Control c = ParseControl("<asp:button text='Click here!' runat='server' />");
      myPlaceholder.Controls.Add(c);
    }
    </script>
  </head>
  <body>
    <form id="form1" runat="server">
      <asp:placeholder id ="myPlaceholder" runat="server" />
    </form>
  </body>
</html>

備註

參數 content 包含使用者控制項,例如您會在 .ascx 檔案中找到。 這個字串不能包含任何程式碼,因為 ParseControl 方法永遠不會造成編譯。

另請參閱

適用於

.NET Framework 4.8.1 和其他版本
產品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

ParseControl(String, Boolean)

在 ASP.NET 網頁或使用者控制項上,將輸入字串剖析為 Control 物件。

C#
public System.Web.UI.Control ParseControl (string content, bool ignoreParserFilter);

參數

content
String

字串,包含使用者控制項。

ignoreParserFilter
Boolean

指定是否忽略剖析器篩選條件的值。

傳回

已經剖析的控制項。

備註

參數 content 包含使用者控制項, (.ascx 檔案的內容) 。 這個字串不能包含任何程式碼,因為 ParseControl 方法永遠不會造成編譯。

參數 ignoreParserFilter 允許 PageParserFilter 忽略 類別。 ASP.NET PageParserFilter 剖析器會使用此類別來判斷在剖析階段是否允許頁面中的專案。

適用於

.NET Framework 4.8.1 和其他版本
產品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1