Thursday, June 21, 2012

Adding Controls in Run Time

There are many ways to add controls in runtime in ASP.NET, however the one I love is using the placeholder control. Its easy, you can place it inside the div element or td element of a table and add controls without worrying where the controls will appear inside the page. So add a placeholder inside the div element and add controls. Following code snippet shows you how to add Label Control during runtime

Adding Controls in runtime in C# ASP.NET

protected void Page_Load(object sender, EventArgs e) { addControls(); } protected void addControls(); { for(int i=0;i<5;i++) { Label lbl = new Label(); lbl.ID = "lbl" + app_code + "_" + priv_code; lbl.ToolTip = dsPriv.Tables[0].Rows[j][4].ToString(); lbl.Style.Add("margin-left", "40px"); lbl.Text = dsPriv.Tables[0].Rows[j][3].ToString(); PlaceHolder1.Controls.Add(lbl); LiteralControl lCntrl = new LiteralControl("
"); PlaceHolder1.Controls.Add(lCntrl); } }
and that's it

No comments: