RE: Adding Controls to a Page 2005-02-22 - By Robert Hanson
When you add controls dynamically, they need to be added to the page on each page load. Controls added dynamically do not automatically get recreated on postback, so you have to do this yourself. The best place to do this is in the Page_Init() method, because this happens before any postback or viewstate processing, so controls added at this point participate fully in both viewstate restore and postback form processing.
"Boudreau, Duane" <BoudreauD@(protected)> on 02/22/2005 01:27:42 PM
Please respond to aspnet-controlbuilding@(protected)
To: aspnet-controlbuilding@(protected) cc:
Subject: [aspnet-controlbuilding] RE: Adding Controls to a Page
I added a table place holder and added the controls on the fly and it works fine, except when I submit the page the controls no longer exist. Any idea what needs to be done to get the controls to be displayed on the submit page? Also how are the control values accesses in the post back?
int x = 0; while (qryPM.Read()) { TableRow tr = new TableRow(); CheckBox cb = new CheckBox(); cb.ID = "cbxPM:" + qryPM["f1"].ToString(); cb.Text = qryPM["f2"].ToString();
TextBox tb = new TextBox(); tb.ID = "txtPMQty:" + qryPM["f1"].ToString(); tb.CssClass = "inputControl"; tb.Width = System.Web.UI.WebControls.Unit.Parse("30px",System.Globalization.Culture Info.InvariantCulture);
Label lb = new Label(); lb.ID = "lblPM" + x.ToString(); lb.Text = " " + qryPM["f3"].ToString();
TableCell tc1 = new TableCell(); tc1.Controls.Add(cb); tr.Cells.Add(tc1);
TableCell tc2 = new TableCell(); tc2.Controls.Add(tb); tr.Cells.Add(tc2);
TableCell tc3 = new TableCell(); tc3.Controls.Add(lb); tr.Cells.Add(tc3);
tblPM.Rows.Add(tr); x++; }
-----Original Message----- From: Sonu Kapoor [mailto:sonukapoor@(protected)] Sent: Friday, February 18, 2005 6:15 PM To: aspnet-controlbuilding@(protected) Subject: [aspnet-controlbuilding] RE: Adding Controls to a Page
You can also use a PlaceHolder. Something like this:
Label lbl = new Label; lbl.Text = "test";
placeholder.controls.add(lbl);
-- Sonu Kapoor WebSite: http://www.Kapoorsolutions.com Blog: http://www.Kapoorsolutions.com/blog/
On Fri, 18 Feb 2005 18:11:20 -0500, James Shaw <james@(protected)> wrote: > Usually you have a container like an <asp:panel> or <asp:literal> on the > page, and then add controls to it with .Controls.Add() from codebehind > > Best regards, > James Shaw > 2005 Microsoft MVP ASP.NET > www.CoverYourASP.NET > Blogging on ASP.NET > www.CoverYourASP.com > Download it. Run it. Learn it. > www.DozingDogs.com > Over 50 million pages served > > > -----Original Message----- > From: Boudreau, Duane [mailto:BoudreauD@(protected)] > Sent: Friday, February 18, 2005 4:30 PM > To: aspnet-controlbuilding@(protected) > Subject: [aspnet-controlbuilding] Adding Controls to a Page > > Using C#, is there a way to dynamically add controls (labels, textboxes, > etc) to a page in its code behind page? > > If so can someone point me to a tutorial or some sample code? > > Thanks, > Duane > > Need SQL Advice? http://sqladvice.com > Need RegEx Advice? http://regexadvice.com > Need XML Advice? http://xmladvice.com > > Need SQL Advice? http://sqladvice.com > Need RegEx Advice? http://regexadvice.com > Need XML Advice? http://xmladvice.com >
Need SQL Advice? http://sqladvice.com Need RegEx Advice? http://regexadvice.com Need XML Advice? http://xmladvice.com
Need SQL Advice? http://sqladvice.com Need RegEx Advice? http://regexadvice.com Need XML Advice? http://xmladvice.com
Need SQL Advice? http://sqladvice.com Need RegEx Advice? http://regexadvice.com Need XML Advice? http://xmladvice.com
|
|