AttributeCollection.Render (Método)
Ensamblado: System.Web (en system.web.dll)
Parámetros
- writer
Instancia de HtmlTextWriter que escribe la colección de atributos en la secuencia de salida actual.
Esta sección contiene dos ejemplos de código. En el primer ejemplo de código se muestra cómo heredar de la clase WebControl para crear un control personalizado denominado AttribRender que reemplaza el método Render. En el segundo ejemplo de código se muestra la forma de utilizar el control personalizado en una página Web ASP.NET.
En el siguiente ejemplo de código se muestra cómo crear un control personalizado denominado AttribRender que anula al método Render de la clase WebControl sin llamar al método Render de la clase base. En lugar de ello, AttribRender invoca el método Render.
/* 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.
This sample can be used as a code-behind file for a.aspx file.
*/
// Create the package that contains the AttribRender and the
// page that accesses it.
package AC_Render;
import System.*;
import System.Web.UI.*;
import System.Web.UI.WebControls.*;
// This is the custom WebControl class.
public class AttribRender extends WebControl
{
// This is the overridden WebControl.Render method.
protected 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.
get_Attributes().Render(output);
} //Render
} //AttribRender
En el ejemplo de código siguiente se muestra la forma de utilizar el control personalizado AttribRender en una página Web. Se da por hecho que el archivo de código correspondiente al control personalizado está en la carpeta App_Code de la aplicación.
<%@ 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="VJ#" %>
<!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.get_Attributes().Add("Text", "HelloWorld!");
c.get_Attributes().Add("Attribute1", "The value for Attribute1.");
get_Controls().Add(c);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="head1" 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>
Windows 98, Windows 2000 Service Pack 4, Windows CE, Windows Millennium, Windows Mobile para Pocket PC, Windows Mobile para Smartphone, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter
Microsoft .NET Framework 3.0 es compatible con Windows Vista, Microsoft Windows XP SP2 y Windows Server 2003 SP1.