Bhushan Posted April 11, 2006 Share Posted April 11, 2006 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 , Link to comment Share on other sites More sharing options...
aspnetguy Posted April 11, 2006 Share Posted April 11, 2006 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 Link to comment Share on other sites More sharing options...
Bhushan Posted April 12, 2006 Author Share Posted April 12, 2006 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()); } Link to comment Share on other sites More sharing options...
aspnetguy Posted April 12, 2006 Share Posted April 12, 2006 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 Link to comment Share on other sites More sharing options...
ray Posted May 5, 2006 Share Posted May 5, 2006 had the same problem with dynamically created controls, as aspnetguy said, you need to create them with every postback. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now