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.

<ul id="main" class="menu">

        <li><a href="http://www.Home.com">Home</a></li>

        <li><a href="http://WWW.technology.com">Technology</a>

            <ul>

                <li><a href="http://www.c-sharpcorner.com">Android Programing</a></li>

                <li><a href="http://www.c-sharpcorner.com">Android Programing</a></li>

            </ul>

        </li>

        <li><a href="http://www.help.com">Help</a>

            <ul>

                <li><a href="http://www.vbdotnetheaven.com">Contact Us</a></li>

                <li><a href="http://www.dotnetheaven.com">Advertise with us</a></li>

                <li><a href="http://www.microsoft.com">Sitemap</a></li>

            </ul>

        </li>

    </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.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.Data;

using System.Web.UI.HtmlControls;

 

public partial class _Default : System.Web.UI.Page

{

    SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=master;uid=sa; pwd=Micr0s0ft");

    protected void Page_Load(object sender, EventArgs e)

    {

        SqlDataAdapter da = new SqlDataAdapter("Select TechnologyID, TechnologyName, TechnologyURL from Technology", con);

        DataTable dttc = new DataTable();

        da.Fill(dttc);

        HtmlGenericControl main = UList("Menuid", "menu"); // This defines <ul> Tag

        foreach (DataRow row in dttc.Rows)

        {

            da = new SqlDataAdapter("select TechnologyCategoryID,TechnologyCategoryName,TechnologyCategoryURL from TechnologyCategory where TechnologyID=" + row["TechnologyID"].ToString(), con);

            DataTable dtDist = new DataTable();

            da.Fill(dtDist);

            if (dtDist.Rows.Count > 0)

            {

                HtmlGenericControl sub_menu = LIList(row["TechnologyName"].ToString(), row["TechnologyID"].ToString(), row["TechnologyURL"].ToString());   // This defines li Tag in ul(<ul>-><li>)            

                HtmlGenericControl ul = new HtmlGenericControl("ul"); // This defines inner ul Tag of li (<ul>-><li>-><ul>)            

                foreach (DataRow r in dtDist.Rows)

                {

                    ul.Controls.Add(LIList(r["TechnologyCategoryName"].ToString(), r["TechnologyCategoryID"].ToString(), r["TechnologyCategoryURL"].ToString()));

                }

                sub_menu.Controls.Add(ul);

                main.Controls.Add(sub_menu);

            }

            else

            {

                main.Controls.Add(LIList(row["TechnologyName"].ToString(), row["TechnologyID"].ToString(), row["TechnologyURL"].ToString()));

            }

        }

        Panel1.Controls.Add(main);

    }

    private HtmlGenericControl UList(string id, string cssClass) // For <ul> tag

    {

        HtmlGenericControl ul = new HtmlGenericControl("ul");

        ul.ID = id;

        ul.Attributes.Add("class", cssClass);

        return ul;

    }

    private HtmlGenericControl LIList(string innerHtml, string rel, string url) // For <li> tag

    {      

        HtmlGenericControl li = new HtmlGenericControl("li");

        li.Attributes.Add("rel", rel);

        li.InnerHtml = "<a href=" + string.Format("http://{0}", url) + ">" + innerHtml + "</a>";      

        return li;

    }

}

 

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/

Up Next
    Ebook Download
    View all
    Learn
    View all