Condividi tramite


Sintassi dichiarativa per il controllo server HtmlGenericControl

Crea un controllo lato server che viene mappato a un elemento HTML non rappresentato da una classe .NET Framework specifica, ad esempio <body> e <div>.

<span | body | div | font | others 
    EnableViewState="False|True" 
    ID="string" 
    OnDataBinding="OnDataBinding event handler" 
    OnDisposed="OnDisposed" 
    OnInit="OnInit event handler" 
    OnLoad="OnLoad event handler" 
    OnPreRender="OnPreRender event handler" 
    OnServerClick="OnServerClick event handler" 
    OnUnload="OnUnload event handler"
    runat="server" 
    Visible="False|True" >
    contentBetweenTags 
</span | body | div | font | others>

Note

Questo controllo viene creato sul server in risposta ai tag che includono la coppia attributo/valore runat="server" negli elementi che non vengono mappati direttamente a un controllo HTML specifico. Tali elementi includono, tra gli altri, <span>, <body>, <div> e <font>. Il controllo mappa il nome del tag dell'elemento specifico da utilizzare come controllo HTML ad ASP.NET tramite la proprietà TagName. Tale controllo eredita le funzionalità dalla classe HtmlContainerControl, che consente di modificare in modo dinamico il contenuto interno dei tag dei controlli HTML.

È possibile utilizzare un elemento <span> del lato server per visualizzare il testo generato dal codice del gestore eventi tramite l'input dell'utente o da un'origine indicata nel gestore eventi. È inoltre possibile utilizzare l'evento Page_Load per generare testo in un controllo span e attributi di stile HTML per formattare il testo quando viene visualizzato nel browser.

Esempio

Nell'esempio riportato di seguito viene illustrato come generare testo da visualizzare in base all'input dell'utente in un controllo HtmlInputText. HtmlGenericControl, creato mediante la dichiarazione dell'elemento <span> nella pagina, consente all'elemento <span> l'accesso alla proprietà InnerHtml. In questo modo sarà possibile modificare la stringa di testo assegnata all'elemento <span>.

<%@ Page Language="VB" AutoEventWireup="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>HtmlGenericControl Control</title>

   <script runat="server">
      Sub SubmitBtn_Click(Source As Object, e As EventArgs)
         MySpan.InnerHtml = "Welcome to ASP.NET, " & myText.Value & "."
      End Sub
   </script>

</head>
<body>
   <form id="myForm" runat="server">
   <br />
   Enter your name here: 
   <input type="text" id="myText" runat="server" />
   <br /><br />
   <input id="Submit1" type="submit" value="Click Here!"
          onserverclick="SubmitBtn_Click" runat="server" />
   <br /><br />
   <b><span id="MySpan" runat="server"/></b>
   </form>
</body>
</html>
  <%@ Page Language="C#" AutoEventWireup="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>HtmlGenericControl Control</title>

   <script runat="server">
      void SubmitBtn_Click(object Source, EventArgs e) 
      {
         MySpan.InnerHtml = "Welcome to ASP.NET, " + myText.Value + ".";
      }
   </script>

</head>
<body>
   <form id="myForm" runat="server">
   <br />Enter your name here: 
   <input type="text" id="myText" runat="server" />
   <br /><br />
   <input id="Submit1" type="submit" value="Click Here!"
          onserverclick="SubmitBtn_Click" runat="server" />
   <br /><br />
   <b><span id="MySpan" runat="server"/></b>
   </form>
</body>
</html>

Nell'esempio riportato di seguito viene illustrato come utilizzare un controllo HtmlGenericControl per consentire all'utente la modifica del colore di sfondo di una pagina. Viene inoltre descritto l'utilizzo della classe AttributeCollection per accedere a livello di codice agli attributi che possono essere dichiarati su qualsiasi controllo HTML.

<%@ Page Language="VB" AutoEventWireup="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>HtmlGenericControl Control</title>

   <script runat="server">
      Sub SubmitBtn_Click(Source As Object, e As EventArgs)
         Body.Attributes("bgcolor") = ColorSelect.Value
      End Sub
   </script>

</head>

<body id="Body" runat="server">

   <h3>Updating Styles with the HtmlGenericControl</h3>

   <form id="Form1" runat="server">
      <br />
      Select a background color for the page: <br />
      <select id="ColorSelect" runat="server">
         <option>White</option>
         <option>Wheat</option>
         <option>Gainsboro</option>
         <option>LemonChiffon</option>
      </select>
      <input id="Submit1" type="submit" runat="server" 
             value="Apply" onserverclick="SubmitBtn_Click" />
   </form>
</body>
</html>
  <%@ Page Language="C#" AutoEventWireup="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>HtmlGenericControl Control</title>

   <script runat="server">
      void SubmitBtn_Click(object Source, EventArgs e) 
      {
         Body.Attributes["bgcolor"] = ColorSelect.Value;
      }
   </script>

</head>

<body id="Body" runat="server">

   <h3>Updating Styles with the HtmlGenericControl</h3>

   <form id="Form1" runat="server">
      <br />
      Select a background color for the page: <br />
      <select id="ColorSelect" runat="server">
         <option>White</option>
         <option>Wheat</option>
         <option>Gainsboro</option>
         <option>LemonChiffon</option>
      </select>
      <input id="Submit1" type="submit" runat="server" 
             value="Apply" onserverclick="SubmitBtn_Click" />
   </form>
</body>
</html>

Vedere anche

Riferimenti

HtmlGenericControl

Altre risorse

Controlli server HTML