Access dynamically created controls

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default Access dynamically created controls

    Hi there,
    I read a lot about this issue but still got no clear answer that
    solves my problem.

    I've a Web User Control with a placeholder called phProperties.
    I create a form with a number of Textboxes dynamically from values in
    a database and add them to the phProperties placeholder. The number of
    Textboxes depends on the template the user clicked. Each template has
    his own number of properties.
    I store the controlnames in a hashtable called 'templatepropertynames'

    After the user clicks a button, I want to store the values from the
    textboxes in a database.

    The problem is that I can't get to the controls in the Button_click
    event.
    I tried with FindControl but with no result.
    When debugging the value of myEnumerator.value in the button_click
    event is correct which is the name of the control but:
    The 'value' of
    phProperties.FindControl(myEnumerator.value) is always 'nothing'
    in the button_click event where I expected a reference to the
    textbox.


    How can I retreive the values from the Textboxes in the Button_click
    event.

    Here my code:

    Private Sub CreatePropertyFields(ByVal templateid As Integer)
    Dim getproperties As New SqlCommand("select * from
    TemplateProperties where templateID=" & templateid, myConnection)
    Dim propertyreader As SqlDataReader
    myConnection.Open()
    propertyreader = getproperties.ExecuteReader
    Dim templatepropertynames As New Hashtable

    While propertyreader.Read
    templatepropertynames(propertyreader.Item("propert yID")) =
    propertyreader.Item("propertyName")
    'create label for textbox
    Dim propertylbl As New Label
    propertylbl.Text =
    propertyreader.Item("propertyName").trim
    propertylbl.Width = Unit.Pixel(200)
    Me.phProperties.Controls.Add(propertylbl)

    'create the Textbox
    Dim newcontrol As New TextBox
    newcontrol.ID =
    propertyreader.Item("propertyName").trim
    Me.phProperties.Controls.Add(newcontrol)

    'create seperator
    Dim seperatorlbl As New Label
    seperatorlbl.Text = "<br>"
    Me.phProperties.Controls.Add(seperatorlbl)

    End While
    Session("propertynames") = templatepropertynames
    myConnection.Close()
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles btnNaar4.Click
    Dim templatepropertynames As New Hashtable
    templatepropertynames = Session("propertynames")
    Dim myEnumerator As IDictionaryEnumerator =
    templatepropertynames.GetEnumerator()
    While myEnumerator.MoveNext()
    Dim Textboxvalue As String =
    CType(phProperties.FindControl(myEnumerator.value) , Textbox).Text
    'insert textboxvalue in the database here
    End While

    End Sub

    Thanks in advance
    Peter
    T-Bone Guest

  2. Similar Questions and Discussions

    1. Eventhandling from dynamically created controls
      I have some questions reguarding event handling of dynamically added controls. An example scenario (se code below): I have one button declared...
    2. Dynamically created user controls
      In ASP.Net, I am working with some in-house software that dynamically creates a form based on rows in a database table. For example, most pages...
    3. Referencing Dynamically created controls
      Can anyone tell me how to reference a a control, e.g. a control I have added to the controls collection without specifically naming it. Also can...
    4. retrieving values from dynamically created controls
      Hi, Someone please clear this for me! I display a bunch of button controls on a page dynamically, adding them to a placeholder control in my...
    5. get formvalues - dynamically created controls
      C.H., You could loop through them using their index. Instead of using FindControl and the control's name you could just use the index of the...
  3. #2

    Default Re: Access dynamically created controls

    "T-Bone" <sub@pox.nl> wrote in message
    news:4378c773.0407020120.49a9b8c0@posting.google.c om...
    > Hi there,
    > I read a lot about this issue but still got no clear answer that
    > solves my problem.
    >
    > I've a Web User Control with a placeholder called phProperties.
    > I create a form with a number of Textboxes dynamically from values in
    > a database and add them to the phProperties placeholder. The number of
    > Textboxes depends on the template the user clicked. Each template has
    > his own number of properties.
    > I store the controlnames in a hashtable called 'templatepropertynames'
    >
    > After the user clicks a button, I want to store the values from the
    > textboxes in a database.
    Dynamically added controls need to be added on every request, and in the
    exact same order.
    --
    John Saunders
    johnwsaundersiii at hotmail


    John Saunders Guest

  4. #3

    Default Re: Access dynamically created controls

    "John Saunders" <johnwsaundersiii@notcoldmail.com> wrote in message news:<emsDZRFYEHA.712@TK2MSFTNGP11.phx.gbl>...
    > "T-Bone" <sub@pox.nl> wrote in message
    > news:4378c773.0407020120.49a9b8c0@posting.google.c om...
    > > Hi there,
    > > I read a lot about this issue but still got no clear answer that
    > > solves my problem.
    > >
    > > I've a Web User Control with a placeholder called phProperties.
    > > I create a form with a number of Textboxes dynamically from values in
    > > a database and add them to the phProperties placeholder. The number of
    > > Textboxes depends on the template the user clicked. Each template has
    > > his own number of properties.
    > > I store the controlnames in a hashtable called 'templatepropertynames'
    > >
    > > After the user clicks a button, I want to store the values from the
    > > textboxes in a database.
    >
    > Dynamically added controls need to be added on every request, and in the
    > exact same order.

    OK, but what about the values. If I create the controls on every page
    request the values will be empty every time the page is loaded and I
    never can get to the submitted values. or not?
    T-Bone Guest

  5. #4

    Default Re: Access dynamically created controls

    Ok, but what about the values. If I create the values on every page
    request, the entered values in the Textboxes before the button click
    will be gone. Or not?


    *** Sent via Devdex [url]http://www.devdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    P K Guest

  6. #5

    Default Re: Access dynamically created controls

    "John Saunders" <johnwsaundersiii@notcoldmail.com> wrote in message news:<emsDZRFYEHA.712@TK2MSFTNGP11.phx.gbl>...
    > "T-Bone" <sub@pox.nl> wrote in message
    > news:4378c773.0407020120.49a9b8c0@posting.google.c om...
    > > Hi there,
    > > I read a lot about this issue but still got no clear answer that
    > > solves my problem.
    > >
    > > I've a Web User Control with a placeholder called phProperties.
    > > I create a form with a number of Textboxes dynamically from values in
    > > a database and add them to the phProperties placeholder. The number of
    > > Textboxes depends on the template the user clicked. Each template has
    > > his own number of properties.
    > > I store the controlnames in a hashtable called 'templatepropertynames'
    > >
    > > After the user clicks a button, I want to store the values from the
    > > textboxes in a database.
    >
    > Dynamically added controls need to be added on every request, and in the
    > exact same order.
    Ok, but what about the values in the Textbox. If I create the controls
    on every page_load the values will be empty every time. How can I get
    to the submitted values?
    T-Bone Guest

  7. #6

    Default Re: Access dynamically created controls

    "T-Bone" <sub@pox.nl> wrote in message
    news:4378c773.0407042304.289ab5bf@posting.google.c om...
    > "John Saunders" <johnwsaundersiii@notcoldmail.com> wrote in message
    news:<emsDZRFYEHA.712@TK2MSFTNGP11.phx.gbl>...
    > > "T-Bone" <sub@pox.nl> wrote in message
    > > news:4378c773.0407020120.49a9b8c0@posting.google.c om...
    > > > Hi there,
    > > > I read a lot about this issue but still got no clear answer that
    > > > solves my problem.
    > > >
    > > > I've a Web User Control with a placeholder called phProperties.
    > > > I create a form with a number of Textboxes dynamically from values in
    > > > a database and add them to the phProperties placeholder. The number of
    > > > Textboxes depends on the template the user clicked. Each template has
    > > > his own number of properties.
    > > > I store the controlnames in a hashtable called 'templatepropertynames'
    > > >
    > > > After the user clicks a button, I want to store the values from the
    > > > textboxes in a database.
    > >
    > > Dynamically added controls need to be added on every request, and in the
    > > exact same order.
    >
    > Ok, but what about the values in the Textbox. If I create the controls
    > on every page_load the values will be empty every time. How can I get
    > to the submitted values?
    ViewState will take care of that for you.
    --
    John Saunders
    johnwsaundersiii at hotmail


    John Saunders Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139