Partager via


HtmlGenericControl Server Control Declarative Syntax 

Creates a server-side control that maps to an HTML element not represented by a specific .NET Framework class, such as <body> and <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>

Remarks

This control is created on the server in response to tags that include the runat="server" attribute/value pair in elements that do not map directly to a specific HTML control. These elements include the <span>, <body>, <div>, and <font> elements, among others. The control maps the tag name of the particular element to be used as an HTML control to ASP.NET through the TagName property. This control inherits functionality from the HtmlContainerControl class, which allows you to dynamically change inner content of HTML control tags.

You can use a server-side <span> element to display text generated by event-handler code, whether through user input or from a source you designate in your event handler. You can also use the Page_Load event to generate text in a span control and HTML style attributes to format the text when it is displayed in the browser.

Example

The following example shows how you can generate text to display based on user input in an HtmlInputText control. The HtmlGenericControl, which is created by declaring the <span> element on the page, provides the <span> element with access to the InnerHtml property. This allows you to manipulate the text string assigned to the <span> element.

<%@ 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>

The following example shows how you can use an HtmlGenericControl to allow a user to modify a page's background color. It also shows how to use the AttributeCollection class to programmatically access the attributes that can be declared on any HTML control.

<%@ 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>

See Also

Reference

HtmlGenericControl

Other Resources

HTML Server Controls