다음을 통해 공유


AttributeCollection.Render(HtmlTextWriter) 메서드

정의

컬렉션이 속하는 컨트롤에 대한 지정된 HtmlTextWriter 출력 스트림에 특성 컬렉션을 씁니다.

public:
 void Render(System::Web::UI::HtmlTextWriter ^ writer);
public void Render (System.Web.UI.HtmlTextWriter writer);
member this.Render : System.Web.UI.HtmlTextWriter -> unit
Public Sub Render (writer As HtmlTextWriter)

매개 변수

writer
HtmlTextWriter

현재 출력 스트림에 특성 컬렉션을 쓰는 HtmlTextWriter 인스턴스입니다.

예제

이 섹션에는 두 코드 예제가 있습니다. 첫 번째 코드 예제에서는 클래스에서 상속하여 메서드를 재정의 WebControl 하는 사용자 AttribRender 지정 컨트롤을 Render 만드는 방법을 보여 줍니다. 두 번째 코드 예제에서는 ASP.NET 웹 페이지에서 사용자 지정 컨트롤을 사용 하는 방법을 보여 있습니다.

다음 예제에서는 기본 클래스의 메서드를 호출 하지 않고 클래스의 메서드를 재정의 WebControl 하는 명명 된 AttribRender 사용자 지정 컨트롤을 Render 만드는 방법을 보여 Render 줍니다. 대신 메서드 AttribRenderRender 호출합니다.

/* Create a custom WebControl class, named AttribRender, that overrides
   the Render method to write two introductory strings. Then call the
   AttributeCollection.Render method, which allows the control to write the
   attribute values that are added to it when it is included in a page.
*/

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Permissions;

// Create the namespace that contains the AttribRender and the
// page that accesses it.
namespace AC_Render
{
   // This is the custom WebControl class.
   [AspNetHostingPermission(SecurityAction.Demand,
      Level=AspNetHostingPermissionLevel.Minimal)]
   public class AttribRender : WebControl
   {
      // This is the overridden WebControl.Render method.
      protected override void Render(HtmlTextWriter output)
      {
         output.Write("<h2>An AttributeCollection.Render Method Example</h2>");
         output.Write("The attributes, and their values, added to the ctl1 control are <br><br>");
         // This is the AttributeCollection.Render method call. When the
         // page that contains this control is requested, the
         // attributes that the page adds, and their values,
         // are rendered to the page.
         Attributes.Render(output);
      }
   }
}
' Create a custom WebControl class, named AttribRender, that overrides 
' the Render method to write two introductory strings. Then call the
' AttributeCollection.Render method, which allows the control to write the
' attribute values that are added to it when it is included in a page.
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Security.Permissions

' Create the namespace that contains the AttribRender and the
' page that accesses it.
Namespace AC_Render

 ' This is the custom WebControl class.
    <AspNetHostingPermission(SecurityAction.Demand, _
      Level:=AspNetHostingPermissionLevel.Minimal)> _
    Public Class AttribRender
        Inherits WebControl

        ' This is the overridden WebControl.Render method.
        Protected Overrides Sub Render(ByVal output As HtmlTextWriter)
            output.Write("<h2>An AttributeCollection.Render Method Example</h2>")
            output.Write("The attributes, and their values, added to the ctl1 control are <br><br>")
            ' This is the AttributeCollection.Render method call. When the
            ' page that contains this control is requested, the
            ' attributes that the page adds, and their values,
            ' are rendered to the page.
            Attributes.Render(output)
        End Sub
    End Class

End Namespace 'AC_Render

다음 예제에서는 웹 페이지에서 사용자 지정 컨트롤을 AttribRender 사용하는 방법을 보여줍니다. 애플리케이션의 App_Code 폴더에 사용자 지정 컨트롤에 대 한 코드 파일 임을 가정 합니다.

<%@ 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)
  {
    AC_Render.AttribRender c = new AC_Render.AttribRender();
    c.Attributes.Add("Text", "Hello World!");
    c.Attributes.Add("Attribute1", "The value for Attribute1.");
    Place1.Controls.Add(c);
    
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>AttributeCollection Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:PlaceHolder id="Place1" runat="server"></asp:PlaceHolder>
    </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)

    Dim c As New AC_Render.AttribRender()
    c.Attributes.Add("Text", "Hello World!")
    c.Attributes.Add("Attribute1", "The value for Attribute1.")
    Place1.Controls.Add(c)

  End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>AttributeCollection Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:PlaceHolder id="Place1" runat="server"></asp:PlaceHolder>
    </div>
    </form>
</body>
</html>

적용 대상

추가 정보