Iterate through dynamically created form fields

Discussion in 'ASP .Net' started by guido, Jun 15, 2007.

  1. guido

    guido Guest

    I've written a user control to add form fields to a aspx page:
    Private Sub buildFields()
    'Label1.Text = "building fields at " & System.DateTime.Now()
    Dim themeIDField As New TextBox()
    themeIDField.ID = "theme_id"
    themeNamePlaceholder.Controls.Add(themeIDField)
    End sub

    ....and call this at page_load. I now need to get the name value pairs
    of these fields to submit to a DB procedure. I've tried using
    request.form iteration:

    For i As Integer = 1 To Request.Form.Count - 1
    Label1.Text += Request.Form.Keys(i) + " = " +
    Request.Form.Item(i) + "<br/>"
    Next

    ....and this works almost - Request.Form.Keys(i) yields "ctl08$
    [field_name here] = [value here]"

    Where has ctl08$ come from? Is it related to the placeholder that i
    replace when building the field?

    <td class="cell_label" style="width: 120px; height: 18px">
    Theme Name</td>
    <td class="cell_info" style="width: 349px; height: 18px">
    <asp:placeHolder ID="themeNamePlaceholder"
    runat="server"></asp:placeHolder>
    </td>
     
    guido, Jun 15, 2007
    #1
    1. Advertising

  2. Hi

    Placeholder does not prefix control IDs with its ID as it is not naming container,
    but UserControl does. Therefore Ithink that ctrl08 comes from UserControl.

    Anyway, it is better to avoid direct Request.Form access and retrieve submitted
    values from the controls you have created. You need to populate placeholder
    on postback with the same controls.

    -yuriy

    > I've written a user control to add form fields to a aspx page:
    > Private Sub buildFields()
    > 'Label1.Text = "building fields at " & System.DateTime.Now()
    > Dim themeIDField As New TextBox()
    > themeIDField.ID = "theme_id"
    > themeNamePlaceholder.Controls.Add(themeIDField)
    > End sub
    > ...and call this at page_load. I now need to get the name value pairs
    > of these fields to submit to a DB procedure. I've tried using
    > request.form iteration:
    >
    > For i As Integer = 1 To Request.Form.Count - 1
    > Label1.Text += Request.Form.Keys(i) + " = " +
    > Request.Form.Item(i) + "<br/>"
    > Next
    >
    > ...and this works almost - Request.Form.Keys(i) yields "ctl08$
    > [field_name here] = [value here]"
    >
    > Where has ctl08$ come from? Is it related to the placeholder that i
    > replace when building the field?
    >
    > <td class="cell_label" style="width: 120px; height: 18px">
    > Theme Name</td>
    > <td class="cell_info" style="width: 349px; height: 18px">
    > <asp:placeHolder ID="themeNamePlaceholder"
    > runat="server"></asp:placeHolder>
    > </td>
    >
     
    Yuriy Solodkyy, Jun 15, 2007
    #2
    1. Advertising

  3. guido

    guido Guest

    On 15 Jun, 15:31, Yuriy Solodkyy <> wrote:
    > Hi
    >
    > Placeholder does not prefix control IDs with its ID as it is not naming container,
    > but UserControl does. Therefore Ithink that ctrl08 comes from UserControl.
    >
    > Anyway, it is better to avoid direct Request.Form access and retrieve submitted
    > values from the controls you have created. You need to populate placeholder
    > on postback with the same controls.
    >
    > -yuriy
    >
    >
    >
    > > I've written a user control to add form fields to a aspx page:
    > > Private Sub buildFields()
    > > 'Label1.Text = "building fields at " & System.DateTime.Now()
    > > Dim themeIDField As New TextBox()
    > > themeIDField.ID = "theme_id"
    > > themeNamePlaceholder.Controls.Add(themeIDField)
    > > End sub
    > > ...and call this at page_load. I now need to get the name value pairs
    > > of these fields to submit to a DB procedure. I've tried using
    > > request.form iteration:

    >
    > > For i As Integer = 1 To Request.Form.Count - 1
    > > Label1.Text += Request.Form.Keys(i) + " = " +
    > > Request.Form.Item(i) + "<br/>"
    > > Next

    >
    > > ...and this works almost - Request.Form.Keys(i) yields "ctl08$
    > > [field_name here] = [value here]"

    >
    > > Where has ctl08$ come from? Is it related to the placeholder that i
    > > replace when building the field?

    >
    > > <td class="cell_label" style="width: 120px; height: 18px">
    > > Theme Name</td>
    > > <td class="cell_info" style="width: 349px; height: 18px">
    > > <asp:placeHolder ID="themeNamePlaceholder"
    > > runat="server"></asp:placeHolder>
    > > </td>- Hide quoted text -

    >
    > - Show quoted text -


    Hi - thanks for the reply. Could you give me an example of how to
    retrieve the values submitted from the form on postback please?
     
    guido, Jun 16, 2007
    #3
  4. here is sample of user control with dynamic text boxes created on Page_Load.
    If possible, you should create controls in Page_Init.

    WebUserControl.ascx.vb:
    Partial Class WebUserControl
    Inherits System.Web.UI.UserControl

    Dim DynamicControls(10) As Control


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Handles Me.Load
    For i As Integer = 0 To 9
    Dim textBox As New TextBox
    textBox.ID = "c_" & i
    c_placeHolder.Controls.Add(textBox)
    DynamicControls(i) = textBox
    Next
    End Sub

    Protected Sub vb(ByVal sender As Object, ByVal e As System.EventArgs)
    Handles c_button.Click
    c_label.Text = ""
    For i As Integer = 0 To 9
    Dim textBox As TextBox = DynamicControls(i)
    c_label.Text += String.Format("{0}={1} ", textBox.ID, textBox.Text)
    Next
    End Sub
    End Class

    WebUserControl.ascx:
    <%@ Control Language="VB" AutoEventWireup="false" CodeFile="WebUserControl.ascx.vb"
    Inherits="WebUserControl" %>
    <asp:Label ID="c_label" runat="server" Text="Label"></asp:Label>
    <asp:Button ID="c_button" runat="server" Text="Button" />
    <br />
    <asp:placeHolder ID="c_placeHolder" runat="server"></asp:placeHolder>


    > On 15 Jun, 15:31, Yuriy Solodkyy <> wrote:
    >
    >> Hi
    >>
    >> Placeholder does not prefix control IDs with its ID as it is not
    >> naming container, but UserControl does. Therefore Ithink that ctrl08
    >> comes from UserControl.
    >>
    >> Anyway, it is better to avoid direct Request.Form access and retrieve
    >> submitted
    >> values from the controls you have created. You need to populate
    >> placeholder
    >> on postback with the same controls.
    >> -yuriy
    >>
    >>> I've written a user control to add form fields to a aspx page:
    >>> Private Sub buildFields()
    >>> 'Label1.Text = "building fields at " & System.DateTime.Now()
    >>> Dim themeIDField As New TextBox()
    >>> themeIDField.ID = "theme_id"
    >>> themeNamePlaceholder.Controls.Add(themeIDField)
    >>> End sub
    >>> ...and call this at page_load. I now need to get the name value
    >>> pairs
    >>> of these fields to submit to a DB procedure. I've tried using
    >>> request.form iteration:
    >>> For i As Integer = 1 To Request.Form.Count - 1
    >>> Label1.Text += Request.Form.Keys(i) + " = " +
    >>> Request.Form.Item(i) + "<br/>"
    >>> Next
    >>> ...and this works almost - Request.Form.Keys(i) yields "ctl08$
    >>> [field_name here] = [value here]"
    >>>
    >>> Where has ctl08$ come from? Is it related to the placeholder that i
    >>> replace when building the field?
    >>>
    >>> <td class="cell_label" style="width: 120px; height: 18px">
    >>> Theme Name</td>
    >>> <td class="cell_info" style="width: 349px; height: 18px">
    >>> <asp:placeHolder ID="themeNamePlaceholder"
    >>> runat="server"></asp:placeHolder>
    >>> </td>- Hide quoted text -

    >> - Show quoted text -
    >>

    > Hi - thanks for the reply. Could you give me an example of how to
    > retrieve the values submitted from the form on postback please?
    >
     
    Yuriy Solodkyy, Jun 16, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gogo
    Replies:
    1
    Views:
    2,128
    Sudsy
    Sep 4, 2003
  2. runescience
    Replies:
    0
    Views:
    1,479
    runescience
    Feb 9, 2006
  3. John
    Replies:
    4
    Views:
    944
    RedGrittyBrick
    Apr 1, 2008
  4. msimmons
    Replies:
    0
    Views:
    523
    msimmons
    Jul 16, 2009
  5. Thomas
    Replies:
    6
    Views:
    181
Loading...

Share This Page