HtmlGenericControl

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
      id="programmaticID" 
      runat="server" >
   Contentbetweentags
</span | body | div | font | others>

Remarks

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.

To generate text on a page dynamically from user input using the <span> element

  1. Declare a server-side span control on the page and assign it an id attribute to allow programmatic access to the control.

    <span id="MySpan" runat="server" />
    
  2. Declare an HtmlInputText control and assign it an id attribute to allow programmatic access to the control.

    <input type="text" id="myText" runat="server">
    
  3. Declare an HtmlInputButton control and assign it an id attribute to allow programmatic access to the control. You should also declare an OnServerClick event handler that specifies the action to perform when the button is clicked.

    <input type="submit" Value="Click Here!"
           OnServerClick="SubmitBtn_Click" runat="server">
    
  4. Create event-handler code that manipulates the value entered by the user in the text box to display it through the span control. This code can be placed anywhere on the page in a code-behind file.

    Sub SubmitBtn_Click(sender As Object, e As EventArgs)
       MySpan.InnerHtml = myText.Value
    End Sub
    [C#]
    void SubmitBtn_Click(object sender, EventArgs e) 
    {
       MySpan.InnerHtml = myText.Value;
    }
    

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" %>
<html>
<head>
   <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">
   <p>Enter your name here: 
   <input type="text" id="myText" runat="server">
   <br><br>
   <input type="submit" Value="Click Here!"
          OnServerClick="SubmitBtn_Click" runat="server">
   <br><br>
   <b><span id="MySpan" runat="server"/><b>
   </form>
</body>
</html>
[C#]
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
   <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">
   <p>Enter your name here: 
   <input type="text" id="myText" runat="server">
   <br><br>
   <input 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" %>
<html>
<head>
   <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 runat="server">
      <p>
      Select a background color for the page: <p>
      <select id="ColorSelect" runat="server">
         <option>White</option>
         <option>Wheat</option>
         <option>Gainsboro</option>
         <option>LemonChiffon</option>
      </select>
      <input type="submit" runat="server" 
             Value="Apply" OnServerClick="SubmitBtn_Click">
   </form>
</body>
</html>
[C#]
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
   <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 runat="server">
      <p>
      Select a background color for the page: <p>
      <select id="ColorSelect" runat="server">
         <option>White</option>
         <option>Wheat</option>
         <option>Gainsboro</option>
         <option>LemonChiffon</option>
      </select>
      <input type="submit" runat="server" 
             Value="Apply" OnServerClick="SubmitBtn_Click">
   </form>
</body>
</html>

See Also

ASP.NET Syntax for HTML Controls | HtmlGenericControl Class