Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Problem with dynamic controls

Problem with dynamic controls

Scheduled Pinned Locked Moved ASP.NET
sysadminhelptutorial
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sishya
    wrote on last edited by
    #1

    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.

    L F 2 Replies Last reply
    0
    • S sishya

      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.

      L Offline
      L Offline
      leckey 0
      wrote on last edited by
      #2

      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!

      S 1 Reply Last reply
      0
      • L leckey 0

        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!

        S Offline
        S Offline
        sishya
        wrote on last edited by
        #3

        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.

        M 1 Reply Last reply
        0
        • S sishya

          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.

          M Offline
          M Offline
          mehnazash
          wrote on last edited by
          #4

          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

          S 1 Reply Last reply
          0
          • M mehnazash

            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

            S Offline
            S Offline
            sishya
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • S sishya

              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.

              F Offline
              F Offline
              fredsom
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups