HtmlGenericControl Class in ASP.Net

HtmlGenericControl Class

When you try to dynamically change inner content of a HTML control in ASP.Net then something happens. In this article you will see how to change an inner content of HTML control tags like <ul and <li> dynamically using HtmlGenericControl class. Or the HtmlGenericControl is used to control other HTML elements not specified by a specific HTML server control, like <body>, <div>, <span>, <font>, <p>, <ul>, <li> and so on.

Suppose you create a menu with submenu items in HTML. In this situation the inner content of the HTML control tags will not changed. So you can write HTML tags to make that easy as in the following code.

  1. <ul id="main" class="menu">  
  2.     <li><a href="http://www.Home.com">Home</a></li>  
  3.     <li><a href="http://WWW.technology.com">Technology</a>  
  4.         <ul>  
  5.             <li><a href="http://www.c-sharpcorner.com">Android Programing</a></li>  
  6.             <li><a href="http://www.c-sharpcorner.com">Android Programing</a></li>  
  7.         </ul>  
  8.     </li>  
  9.     <li><a href="http://www.help.com">Help</a>  
  10.         <ul>  
  11.             <li><a href="http://www.vbdotnetheaven.com">Contact Us</a></li>  
  12.             <li><a href="http://www.dotnetheaven.com">Advertise with us</a></li>  
  13.             <li><a href="http://www.microsoft.com">Sitemap</a></li>  
  14.         </ul>  
  15.     </li>  
  16. </ul> 

Press F5 to run it.

HtmlGenericControl- Class-1.jpg 
In the preceding example we see the HTML tags <ul> and <li>. 

Now if you want the inner HTML of <ul> and <li> to be changed you can use the HtmlGenericControl class.

When you create this using SQL Server database the inner content of the HTML control will be changed. The "HtmlGenericControl" class created by declaring the <ul> and <li> elements on the ASP.Net page provides the  <ul> and <li> elements with access to the "InnerHtml" property.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data.SqlClient;  
  8. using System.Data;  
  9. using System.Web.UI.HtmlControls;  
  10. public partial class _Default : System.Web.UI.Page  
  11. {  
  12.     SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=master;uid=sa; pwd=Micr0s0ft");  
  13.     protected void Page_Load(object sender, EventArgs e)  
  14.     {  
  15.         SqlDataAdapter da = new SqlDataAdapter("Select TechnologyID, TechnologyName, TechnologyURL from Technology", con);  
  16.         DataTable dttc = new DataTable();  
  17.         da.Fill(dttc);  
  18.         HtmlGenericControl main = UList("Menuid""menu"); // This defines <ul> Tag  
  19.         foreach (DataRow row in dttc.Rows)  
  20.         {  
  21.             da = new SqlDataAdapter("select TechnologyCategoryID,TechnologyCategoryName,TechnologyCategoryURL from TechnologyCategory where TechnologyID=" + row["TechnologyID"].ToString(), con);  
  22.             DataTable dtDist = new DataTable();  
  23.             da.Fill(dtDist);  
  24.             if (dtDist.Rows.Count > 0)  
  25.             {  
  26.                 HtmlGenericControl sub_menu = LIList(row["TechnologyName"].ToString(), row["TechnologyID"].ToString(), row["TechnologyURL"].ToString());   // This defines li Tag in ul(<ul>-><li>)              
  27.                 HtmlGenericControl ul = new HtmlGenericControl("ul"); // This defines inner ul Tag of li (<ul>-><li>-><ul>)              
  28.                 foreach (DataRow r in dtDist.Rows)  
  29.                 {  
  30.                     ul.Controls.Add(LIList(r["TechnologyCategoryName"].ToString(), r["TechnologyCategoryID"].ToString(), r["TechnologyCategoryURL"].ToString()));  
  31.                 }  
  32.                 sub_menu.Controls.Add(ul);  
  33.                 main.Controls.Add(sub_menu);  
  34.             }  
  35.             else  
  36.             {  
  37.                 main.Controls.Add(LIList(row["TechnologyName"].ToString(), row["TechnologyID"].ToString(), row["TechnologyURL"].ToString()));  
  38.             }  
  39.         }  
  40.         Panel1.Controls.Add(main);  
  41.     }  
  42.     private HtmlGenericControl UList(string id, string cssClass) // For <ul> tag  
  43.     {  
  44.         HtmlGenericControl ul = new HtmlGenericControl("ul");  
  45.         ul.ID = id;  
  46.         ul.Attributes.Add("class", cssClass);  
  47.         return ul;  
  48.     }  
  49.     private HtmlGenericControl LIList(string innerHtml, string rel, string url) // For <li> tag  
  50.     {  
  51.         HtmlGenericControl li = new HtmlGenericControl("li");  
  52.         li.Attributes.Add("rel", rel);  
  53.         li.InnerHtml = "<a href=" + string.Format("http://{0}", url) + ">" + innerHtml + "</a>";        
  54.         return li;  
  55.     }  
  56. } 

To understand the code above, please go through the following link:

http://www.c-sharpcorner.com/UploadFile/rohatash/creating-dynamic-menu-from-database-sql-server-in-Asp-Net/


Similar Articles