asp.net.ph

Skip Navigation LinksHome > Getting Started > ASP.NET Syntax > ASP.NET Syntax for HTML Controls > HtmlGenericControl

HtmlGenericControl Syntax

ASP.NET Syntax   ASP.NET Syntax for HTML Controls


The HtmlGenericControl provides a server control implementation for all other HTML server control tags not directly represented by a specific HTML server control. These include the <body>, <div>, and <span>, as well as headers ( <h1>...<h6> ), and even paragraphs ( <p> ) and list ( <ul>, <ol>, <dl> ) elements, among others.

Declarative Syntax

For information on the individual members of this class, see HtmlGenericControl in the class library.

Syntax Notes

  • This control requires an opening and closing tag ( or an opening tag with a trailing slash /> ).

Working with HtmlGenericControl

An HtmlGenericControl is created on the server in response to tags that include the runat="server" attribute in elements that do not map directly to a specific HTML control.

The control maps the tag name of the particular element to be used as an HTML control to the page framework through the tagName property. This control inherits functionality from the HtmlContainerControl class, which allows to dynamically change the inner content of HTML control tags.

The following samples illustrate use of the HtmlGenericControl.


Updating HTML Styles in Response to User Input

The below example illustrates a simple yet powerful use of an HtmlGenericControl. The sample shows how authors can allow a user to modify the control’s attributes, in this case, the page’s background color. This is done via the ASP.NET AttributeCollection class, which enables programmatic access to the list of attributes declared on an HTML control, in this case, the <body> element.

Updating HTML Styles in Response to User Input
Run Sample | View Source

The sample is implemented as follows.

  1. Declare a <body runat="server"> control with a <form> control that contains:
    • an <HtmlSelect> to accept user input;
    • an <HtmlInputButton> to submit the form.
    <body id="Body" runat="server">
    <form runat="server">
    
       <p>Select a background color for the page: </p>
    
       <select id="ColorSelect" runat="server">
          <option>Aqua</option>
          <option>Bisque</option>
          <option>Chartreuse</option>
          ...
       </select>
    
       <input type=submit runat="server" Value = "Apply">
    
    </form>
    </body>
  2. Within the <head> of the page, define the Page_Load handler.
    <head>
    <script language="C#" runat="server">
    void Page_Load ( object Source, EventArgs e ) {
       Body.Attributes [ "bgcolor" ] =ColorSelect.Value;
    }
    </script>
    </head>

Dynamically Generating Text on a Page

In many applications, you will need to display text generated by some other procedure in your code. You can use any server-side container element for this purpose ( <div>, <span>, <p>, etc. ), and simply assign the text to render via the control’s InnerHtml or InnerText property.

Like standard HTML elements, HTML server controls can use CSS style attributes to format the rendering of the text. This sample also shows how authors can dynamically apply styles to a container control based on user input.

Dynamically Generating Text on a Page
Run Sample | View Source

The sample is implemented as follows.

  1. Within the <body> of the page, declare an HtmlInputText control to accept the text input.
    <p>Please enter your name: <input type=text 
          id="userName" runat="server">
  2. Declare three HtmlInputRadio controls to accept the style input.
    <p>Choose what suits you: <br>
          <input type=radio id="cool" runat=server
             value="background:navy; color:lime">Cool
          <input type=radio id="soso" runat=server
             value="background:gainsboro; color:dimgray">So-So
          <input type=radio id="warm" runat=server
             value="background:maroon; color:gold">Warm
  3. Declare an HtmlInputButton control, designating the method that will handle the button’s onServerClick event.
    <p><input type=submit Value = "Send"  runat="server"
          onServerClick = "greetEarthling">
  4. Declare a server-side <span> control to contain the generated text.
    <p><b><span id="greeting" runat="server" />
    </b>
  5. Within the <head> of the page, define the onServerClick event handler. The method essentially:
    • gets the value entered into the text box and assigns this as the <span> control’s content
    • gets the value selected from the radio button group
    • applies the selected style to the <span> control
    <script language="C#" runat="server">
    string style;
    
    void greetEarthling ( object Source, EventArgs e ) {
       greeting.InnerHtml = "Greetings, earthling " + userName.Value + 
          ", welcome to ASP.NET.";
    
       if ( cool.Checked ) style=cool.Value;
       else if ( soso.Checked ) style=soso.Value;
       else if ( warm.Checked ) style=warm.Value;
    
       greeting.Attributes [ "style" ]  = "padding:10; " + style;
    }
    </script>
See Also

Web Forms Events and Handlers   HtmlGenericControl Class



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note