Problem with dynamic controls
-
Hi, i have written following code TextBox tb = new TextBox(); tb.Text = "ghjhgjh"; Label lbl = new Label(); lbl.Text = "Hello, World!"; lbl.Font.Bold = true; Controls.Add(lbl); Controls.Add(tb); foreach (Control c in Controls) Label1.Text += c.ToString() + " - " + c.ID + "
"; but on running i get the following exception --> Control 'ctl03' of type 'TextBox' must be placed inside a form tag with runat=server. also please explain how to retain the view state of the text box. -
Hi, i have written following code TextBox tb = new TextBox(); tb.Text = "ghjhgjh"; Label lbl = new Label(); lbl.Text = "Hello, World!"; lbl.Font.Bold = true; Controls.Add(lbl); Controls.Add(tb); foreach (Control c in Controls) Label1.Text += c.ToString() + " - " + c.ID + "
"; but on running i get the following exception --> Control 'ctl03' of type 'TextBox' must be placed inside a form tag with runat=server. also please explain how to retain the view state of the text box. -
On the HTML tab of the .aspx page you should see the tags. Make sure that it is within the
tags. I've had that happen before. Within the textbox you can do Hope that helps!
Hi, adding within controls inside form tag is not under my control unless i use addat function. Moreover pls help with retaining the view state .Many say that it should be added in the init ftion.But still doesnt work.Pls write a small snippet if possible because couldnt find a suitable link i require in net.
-
Hi, adding within controls inside form tag is not under my control unless i use addat function. Moreover pls help with retaining the view state .Many say that it should be added in the init ftion.But still doesnt work.Pls write a small snippet if possible because couldnt find a suitable link i require in net.
-
Go to the properties of the text box and make view state as true. View state will be automatically saved. Make sure That textbox is server control. Go to html, make sure that is within
tag and include runat = server hope that helps
hi, i have given this piece of code in page Load() { DDLSortBy=new DropDownList (); DataSet ds=new DataSet (); da.Fill(ds); DDLSortBy.DataSource = ds.tables[0] DDLSortBy.DataTextField = "EmpId"; DDLSortBy.DataValueField = "EmpId"; DDLSortBy.DataBind(); DDLSortBy.SelectedIndex = 0; DDLSortBy .AutoPostBack = true; DDLSortBy.SelectedIndexChanged += new System.EventHandler(DDLSortBy_SelectedIndexChanged); plFrm.Controls.Add(DDLSortBy); } i am able to retain the value(view state) of the text box inspite of the DDL getting initialized each time on postback.I see no difference if initialize the controls on OnInit() function. Then why many articles in net suggest us to dynamically add controls in init ftion() . -- modified at 1:11 Wednesday 28th June, 2006
-
Hi, i have written following code TextBox tb = new TextBox(); tb.Text = "ghjhgjh"; Label lbl = new Label(); lbl.Text = "Hello, World!"; lbl.Font.Bold = true; Controls.Add(lbl); Controls.Add(tb); foreach (Control c in Controls) Label1.Text += c.ToString() + " - " + c.ID + "
"; but on running i get the following exception --> Control 'ctl03' of type 'TextBox' must be placed inside a form tag with runat=server. also please explain how to retain the view state of the text box.I think that it is because you add controls using Controls.add() on its own. It may place the controls somewhere in the page that is not between form. Check that in the html generated through your browser. Try using a placeholder where to add your controls and you will have no problems at all cycling through it getting controls infos.