Jump to content
Sign in to follow this  
Bhushan

Dynamic generation of labels and checkbox

Recommended Posts

Hi, I want to add checkbox and labels dynamically into a HTML table in an aspx page. So that in future, if columns increase in database table, I can insert label checkbox pair dynamically in front end and then can query database. By doing this I could avoid getting into codebehind files.How can I achieve this?. Please giv me a valuable suggestion of urs so that it becomes easy to understand. Thanks in advance, Regards ,

Share this post


Link to post
Share on other sites

You can create a control like this

Label lblMyLabel = new Label();lblMyLabel.ID = "label1";lblMyLabel.Text = "I'm a label!";

You then can add it to the page with

Controls.Add(lblMyLabel);

I have never added dynamic contorls to a table before but I imagine it would be something like this

myTable.Cells[index].Controls.Add(lblMyLabel) //I'm guessing on this

Share this post


Link to post
Share on other sites

Hi ,The below code is successfully generating a new label and a checkbox dynamically.But it's not saving. I mean on every post back, it's lost. Again I made to click on the button to generate.How can I make sure that the generated controls are saved. Do u have any Idea??. Label myl=new Label(); CheckBox ck=new CheckBox(); ck.ID="chkfield"; myl.ID="labelfield"; myl.Text="New Label"; HtmlTable t=(HtmlTable)Page.FindControl("Table3"); HtmlTableRow r=new HtmlTableRow(); HtmlTableCell c1=new HtmlTableCell(); HtmlTableCell c2=new HtmlTableCell(); c1.Controls.Add(myl); c2.Controls.Add(ck); r.Cells.Add(c1); r.Cells.Add(c2); t.Rows.Add®;Please see below for one more problem im facing.Here in first function im adding controls dynamically. In the second function im trying to print the ID of the dynamically created control which is giving me "object reference not set to an instance" error.Please let me know how can I achieve this.private void Button5_Click(object sender, System.EventArgs e) { Label myl=new Label(); CheckBox ck=new CheckBox(); ck.ID="chkfield"; myl.ID="labelfield"; myl.Text="New Label"; HtmlTable t=(HtmlTable)Page.FindControl("Table3"); HtmlTableRow r=new HtmlTableRow(); HtmlTableCell c1=new HtmlTableCell(); HtmlTableCell c2=new HtmlTableCell(); c1.Controls.Add(myl); c2.Controls.Add(ck); r.Cells.Add(c1); r.Cells.Add(c2); t.Rows.Add®; } private void Button6_Click(object sender, System.EventArgs e) { CheckBox ch=(CheckBox)Page.FindControl("chkfield"); Response.Write(ch.ID.ToString()); }

Share this post


Link to post
Share on other sites

You will have to make a function that creates the dynamic controls and on every postback you will need to run that function.Creating dynamic controls is a huge pain to maintain and should be avoided when ever possible :)

Share this post


Link to post
Share on other sites

had the same problem with dynamically created controls, as aspnetguy said, you need to create them with every postback.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...