ASP.Net & C# Create HTML Controls
Dynamically
It is very easy to create HTML controls before running a Web Page. Just Drag and Drop from ToolBox or write HTML code or ASP code for that Web Page (.aspx). But people think that generating HTML controls during runtime is difficult. But it’s quite easy.
There are many methods of generating controls during runtime.
Here is the one.
First thing is that you need to add a Panel by drag and drop it from the toolbox.
After that you need to write the C# code in Page Load event or button event as follows :
Panel1.Controls.Add(new LiteralControl(“<table><tr>”));
Panel1.Controls.Add(new LiteralControl(“<td colspan=2 align=center valign=middle >”));
Panel1.Controls.Add(new LiteralControl(“<table><tr><td>”));
Panel1.Controls.Add(new LiteralControl(“<input id=’Text1′ type=’text’ value=’Ganesh Mohan’ />”));
Panel1.Controls.Add(new LiteralControl(“</td><td>”));
Panel1.Controls.Add(new LiteralControl(“<a href=https://ganeshmohan.wordpress.com>”));
Panel1.Controls.Add(new LiteralControl(“Hi This is Ganesh”));
Panel1.Controls.Add(new LiteralControl(“</a>”));
Panel1.Controls.Add(new LiteralControl(“</td></tr></table>”));
That’s it. Its all Done. Its So Simple isn’t it!
HOW TO WRITE EVENT CODE FOR DYANAMICALLY CREATED HTML CONTROL IN ASP.NET(c#)
EXPLAIN WITH SMALL EXAMPLE(CHECKBOX)
When you place a CheckBox on your Aspx Page…
In that CheckBox Properties there is events…
There is a event named “CheckedChanged”
there you need to write this code…
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
//your code
}
But… you need to make AutoPostBack Property of CheckBox to “True”.
Hope This helps you…
Thanks! This simple solution works.
Efficient code thanks
Muchas gracias!!!
Funciono a la perfeccion!!!
Thank you very much!
It works perfectly!!!
hi ….
my question is:creating panel with textboxes dynamically at runtime using class file..can u tell me how to do tat pls
yes
Panel opnlPresentation = new Panel();
opnlPresentation = (Panel)e.Item.FindControl(“pnlPresentation”);
Table otblCaseDetails = new Table();
otblCaseDetails.ID = “tbl_AdminRecommendation” + ((DataRow)e.Item.DataItem)[“ModuleCategoryId”].ToString();
otblCaseDetails.Style.Add(“Display”, “None”);
TableRow otrCaseDetails = new TableRow();
TableCell otdCaseDetails = new TableCell();
otblCaseDetails.CellSpacing = 0;
otblCaseDetails.CellPadding = 6;
otblCaseDetails.Width = Unit.Pixel(436);
otdCaseDetails.Style.Add(“background-image”, “url(” + Page.ResolveUrl(“~/Images/Misc/top.gif”) + “)”);
otdCaseDetails.Height = Unit.Pixel(27);
otrCaseDetails.Cells.Add(otdCaseDetails);
otblCaseDetails.Rows.Add(otrCaseDetails);
string sPresentation = string.Empty;
_BindCaseData(Convert.ToInt32(((DataRow)e.Item.DataItem)[“Id”].ToString()), ref otblCaseDetails, ref sPresentation);
((Label)e.Item.FindControl(“lblPresentation”)).Text = sPresentation;
Label olblCaseName = new Label();
olblCaseName = (Label)e.Item.FindControl(“lblCaseName”);
olblCaseName.Text = ” ” + ((DataRow)e.Item.DataItem)[“TitleKnown”].ToString() + ““;
olblCaseName.ID = “olblCaseName_AdminRecommendation” + ((DataRow)e.Item.DataItem)[“ModuleCategoryId”].ToString();
olblCaseName.Style.Add(“font-size”, “13px”);
AjaxControlToolkit.HoverMenuExtender oHoverMenuExtender = new AjaxControlToolkit.HoverMenuExtender();
oHoverMenuExtender.TargetControlID = olblCaseName.ID;
oHoverMenuExtender.PopupControlID = otblCaseDetails.ID;
oHoverMenuExtender.BehaviorID = “BehaviorID_AdminRecommendation” + ((DataRow)e.Item.DataItem)[“ModuleCategoryId”].ToString();
oHoverMenuExtender.PopupPosition = AjaxControlToolkit.HoverMenuPopupPosition.Right;
opnlPresentation.Controls.Add(oHoverMenuExtender);
otrCaseDetails = new TableRow();
otdCaseDetails = new TableCell();
otdCaseDetails.Style.Add(“background-image”, “url(” + Page.ResolveUrl(“~/Images/Misc/bottom.gif”) + “)”);
otdCaseDetails.Height = Unit.Pixel(6);
otdCaseDetails.Width = Unit.Pixel(436);
otrCaseDetails.Cells.Add(otdCaseDetails);
otblCaseDetails.Rows.Add(otrCaseDetails);
opnlPresentation.Controls.Add(otblCaseDetails);
Thanks.! nice code
How do you add asp.net controls inside a dynamically created html table?
Hi .. this article works for me fine…. can some please tell me how to bind to a label control in the above mentioned method ..
I am posting my cs code here …
Here am creating controls to the page directly …. now i want to bind a data from the object to label
let say
// my collection comes here
Bill bill=new Bill();
string bno=bill.BillNo;
// the below code goes to my page load
this.Controls.Add(new LiteralControl(“”));
this.Controls.Add(new LiteralControl(“”));
this.Controls.Add(new LiteralControl(“”));
this.Controls.Add(new LiteralControl(“”));
this.Controls.Add(new LiteralControl(“”));
this.Controls.Add(new LiteralControl(“”));
this.Controls.Add(new LiteralControl(“”));
this.Controls.Add(new LiteralControl(“”));
this.Controls.Add(new LiteralControl(“”));
now i want to display the value in the string b to my label …….Please help me in this ..
Thanks In Avdance
Jewel Joy
this.Controls.Add(new LiteralControl(“””));
this.Controls.Add(new LiteralControl(“””));
this.Controls.Add(new LiteralControl(“””));
this.Controls.Add(new LiteralControl(“””));
this.Controls.Add(new LiteralControl(“””));
this.Controls.Add(new LiteralControl(“””));
this.Controls.Add(new LiteralControl(“””));
this.Controls.Add(new LiteralControl(“””));
this.Controls.Add(new LiteralControl(“””));
I really like it whenever people come together and share opinions.
Great website, continue the good work!
Hi there to every body, it’s my first pay a quick visit of this web site; this website contains awesome and truly fine material designed for readers.