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. Working with dynamically created controls

Working with dynamically created controls

Scheduled Pinned Locked Moved ASP.NET
helpquestion
17 Posts 5 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.
  • A Anuradha612

    but i am unable to refer to the text property for textboxes, here is how my code goes, to create control Dim fldMetadata As TextBox fldMetadata = New TextBox fldMetadata.ID = "txtID" Panel1.Controls.Add(fldMetadata) I refer to the controls like this, Dim ctrl As Control For Each ctrl In Panel1.Controls If ctrl.ID Like "txtID" Then 'Here i should be able to refer to the control's text property End If Next thanks

    N Offline
    N Offline
    N a v a n e e t h
    wrote on last edited by
    #6

    Anuradha612 wrote:

    For Each ctrl In Panel1.Controls If ctrl.ID Like "txtID" Then 'Here i should be able to refer to the control's text property

    TextBox txt = (TextBox)ctrl;

    Now use txt for getting the textbox properties.

    All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

    1 Reply Last reply
    0
    • P Paul Conrad

      You then should be able to do ctrl.Text=".... "

      "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #7

      Paul Conrad wrote:

      You then should be able to do ctrl.Text=".... "

      a cast is needed I guess

      All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

      P 1 Reply Last reply
      0
      • P Paul Conrad

        You then should be able to do ctrl.Text=".... "

        "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

        A Offline
        A Offline
        Anuradha612
        wrote on last edited by
        #8

        Thanks, I have tried out, but there is no such property

        1 Reply Last reply
        0
        • N N a v a n e e t h

          Paul Conrad wrote:

          You then should be able to do ctrl.Text=".... "

          a cast is needed I guess

          All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

          P Offline
          P Offline
          Paul Conrad
          wrote on last edited by
          #9

          N a v a n e e t h wrote:

          a cast is needed I guess

          Not really. I just tried it and works fine here.

          "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

          N 1 Reply Last reply
          0
          • N N a v a n e e t h

            Anuradha612 wrote:

            how do i retreive the text stored in text box and value selected in the combo.

            Adding to Paul's post, are you getting some problem when retrieving value ? If you are not getting the value entered, check the event where you create dynamic controls. It should be before ASP.NET loads the viestate. Init would be appropriate for dynamic controls.

            All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

            A Offline
            A Offline
            Anuradha612
            wrote on last edited by
            #10

            Yes, i am unable to retreive the value entered, Pls can you be more clear, with the last post, i am unable to understand,

            N 1 Reply Last reply
            0
            • P Paul Conrad

              N a v a n e e t h wrote:

              a cast is needed I guess

              Not really. I just tried it and works fine here.

              "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

              N Offline
              N Offline
              N a v a n e e t h
              wrote on last edited by
              #11

              Paul Conrad wrote:

              Not really. I just tried it and works fine here.

              I think I miss something. But I am unable to get Text property for Control instance as you told.

              All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

              P 1 Reply Last reply
              0
              • N N a v a n e e t h

                Paul Conrad wrote:

                Not really. I just tried it and works fine here.

                I think I miss something. But I am unable to get Text property for Control instance as you told.

                All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

                P Offline
                P Offline
                Paul Conrad
                wrote on last edited by
                #12

                I made a mistake by doing a winform and not a webform :-O You can get it in a winform but not webform.

                "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                1 Reply Last reply
                0
                • A Anuradha612

                  Hi, I have an appln where in i need to create, text boxes and drop downlists dynamically, the issue here is, how do i retreive the text stored in text box and value selected in the combo. Thanks in advance

                  R Offline
                  R Offline
                  rahul net11
                  wrote on last edited by
                  #13

                  Hi You are dynamically created textbox like this. Dim fldMetadata As TextBox fldMetadata = New TextBox fldMetadata.ID = "txtID" Panel1.Controls.Add(fldMetadata) There should be number of text box. When u creating the control dynamically the u cannot specify same id to all control. u have assign unique id to all textbox in page which u create dynamically. R u creating control dynamically in for loop? Can u paste code here? Regards. Rahul

                  People Laugh on me Because i am Different but i Laugh on them Because they all are same.

                  P 1 Reply Last reply
                  0
                  • A Anuradha612

                    Yes, i am unable to retreive the value entered, Pls can you be more clear, with the last post, i am unable to understand,

                    N Offline
                    N Offline
                    N a v a n e e t h
                    wrote on last edited by
                    #14

                    This is a viewstate issue. Hope you know about viewstates. It's mechanism used to keep data across postbacks. For solving the problem, override oninit event and add your controls over there. Then ASP.NET will maintain viewstate for the control and you can get it from Controls collection.

                    All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

                    1 Reply Last reply
                    0
                    • A Anuradha612

                      Hi, I have an appln where in i need to create, text boxes and drop downlists dynamically, the issue here is, how do i retreive the text stored in text box and value selected in the combo. Thanks in advance

                      I Offline
                      I Offline
                      Imran Khan Pathan
                      wrote on last edited by
                      #15

                      Look at this example I create two textboxes and dropdownlist on page load event. I have one Button and one placeholder on the page. On page load event,add all dynamically created in place holder and on button click event, I get values of the textboxes and selected value of dropdownlist. protected void Page_Load(object sender, EventArgs e) { CreateControls(); } public void CreateControls() { TextBox MyTxt1=new TextBox(); MyTxt1.ID = "MyTxt1"; TextBox MyTxt2 = new TextBox(); MyTxt2.ID = "MyTxt2"; DropDownList MyDDL1 = new DropDownList(); MyDDL1.ID = "MyDDL1"; MyDDL1.Items.Add(new ListItem("1", "1")); MyDDL1.Items.Add(new ListItem("2", "2")); DropDownList MyDDL2 = new DropDownList(); MyDDL2.ID = "MyDDL2"; MyDDL2.Items.Add(new ListItem("1", "1")); MyDDL2.Items.Add(new ListItem("2", "2")); PL.Controls.Add(MyTxt1); PL.Controls.Add(MyTxt2); PL.Controls.Add(MyDDL1); PL.Controls.Add(MyDDL2); } protected void MyButton_Click(object sender, EventArgs e) { string myText1 = ((TextBox)this.Page.FindControl("MyTxt1")).Text; string myText2 = ((TextBox)this.Page.FindControl("MyTxt2")).Text; string myDDL1 = ((DropDownList)this.Page.FindControl("MyDDL1")).SelectedValue; string myDDL2 = ((DropDownList)this.Page.FindControl("MyDDL2")).SelectedValue; Response.Write("Value 1 : " + myText1 + " Value 2: " + myText2 + " Value 3 :" + myDDL1 + " Value 4: " + myDDL2); }

                      please don't forget to vote on the post that helped you.

                      P 1 Reply Last reply
                      0
                      • R rahul net11

                        Hi You are dynamically created textbox like this. Dim fldMetadata As TextBox fldMetadata = New TextBox fldMetadata.ID = "txtID" Panel1.Controls.Add(fldMetadata) There should be number of text box. When u creating the control dynamically the u cannot specify same id to all control. u have assign unique id to all textbox in page which u create dynamically. R u creating control dynamically in for loop? Can u paste code here? Regards. Rahul

                        People Laugh on me Because i am Different but i Laugh on them Because they all are same.

                        P Offline
                        P Offline
                        Paul Conrad
                        wrote on last edited by
                        #16

                        rahul.net11 wrote:

                        Can u paste code here?

                        He did, in his second post[^] in the thread.

                        "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                        1 Reply Last reply
                        0
                        • I Imran Khan Pathan

                          Look at this example I create two textboxes and dropdownlist on page load event. I have one Button and one placeholder on the page. On page load event,add all dynamically created in place holder and on button click event, I get values of the textboxes and selected value of dropdownlist. protected void Page_Load(object sender, EventArgs e) { CreateControls(); } public void CreateControls() { TextBox MyTxt1=new TextBox(); MyTxt1.ID = "MyTxt1"; TextBox MyTxt2 = new TextBox(); MyTxt2.ID = "MyTxt2"; DropDownList MyDDL1 = new DropDownList(); MyDDL1.ID = "MyDDL1"; MyDDL1.Items.Add(new ListItem("1", "1")); MyDDL1.Items.Add(new ListItem("2", "2")); DropDownList MyDDL2 = new DropDownList(); MyDDL2.ID = "MyDDL2"; MyDDL2.Items.Add(new ListItem("1", "1")); MyDDL2.Items.Add(new ListItem("2", "2")); PL.Controls.Add(MyTxt1); PL.Controls.Add(MyTxt2); PL.Controls.Add(MyDDL1); PL.Controls.Add(MyDDL2); } protected void MyButton_Click(object sender, EventArgs e) { string myText1 = ((TextBox)this.Page.FindControl("MyTxt1")).Text; string myText2 = ((TextBox)this.Page.FindControl("MyTxt2")).Text; string myDDL1 = ((DropDownList)this.Page.FindControl("MyDDL1")).SelectedValue; string myDDL2 = ((DropDownList)this.Page.FindControl("MyDDL2")).SelectedValue; Response.Write("Value 1 : " + myText1 + " Value 2: " + myText2 + " Value 3 :" + myDDL1 + " Value 4: " + myDDL2); }

                          please don't forget to vote on the post that helped you.

                          P Offline
                          P Offline
                          Paul Conrad
                          wrote on last edited by
                          #17

                          Why the example in C#? The OP posted his code and he was doing it in VB. See his second post[^] in the thread...

                          "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                          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