Asp.net Create Html Controls

Asp.net Create Html Controls Asp.net HtmlGenerci Controls

Do you want to create Html controls dynamically? Here are we will see very basic Html Generic Controls. We will create Div,P,and button controls of HTML.

Let's see how to create html meta tag using Html Generic control class.

  protected void Button1_Click(object sender, EventArgs e)
  {
    HtmlGenericControl meta = new HtmlGenericControl("meta");
    meta.Attributes.Add("name","description");
    meta.Attributes.Add("content","This is meta descccription");       
 
    Header.Controls.Add(meta);
  }
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class HtmlGenericControlExamples : System.Web.UI.Page
{
  protected void Button1_Click(object sender, EventArgs e)
  {
    HtmlGenericControl button = new HtmlGenericControl("input");
    button.Attributes.Add("style", "height:20px;width:120px;");
    button.Attributes.Add("value", "Button control");
    button.Attributes.Add("type", "button");
    button.ID = "button1";


    PlaceHolder1.Controls.Add(button);
  }


  protected void Button2_Click(object sender, EventArgs e)
  {
    HtmlGenericControl divControl = new HtmlGenericControl("div");
    divControl.Attributes.Add("style", "height:220px;width:520px;");    
    divControl.InnerText = "This is div text";
    divControl.ID = "sampleDiv";
    
    PlaceHolder1.Controls.Add(divControl);

  }
  protected void Button3_Click(object sender, EventArgs e)
  {
    HtmlGenericControl pControl = new HtmlGenericControl("p");
    pControl.Attributes.Add("style", "height:120px;width:220px;");
    pControl.InnerText = "This text inside Paragraph";
    pControl.ID = "sampleP";

    PlaceHolder1.Controls.Add(pControl);
  }
}

Tags:

html without using tables tableless html table less html css tableless layoutsAsp.net How to display HTML contentsAsp.net Create Folder On ServerASP.net HTML5 Generate Article TagC# HtmlTextWriter ExampleBasic HTML5 TagsHow To Access Div, Iframe Html Controls Using code behindAsp.net access html control textbox element value at code behindASp.net 2.0 step by step Membership Provider HTML5 With DataBaseCreate contact us form

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.