Iterate through dynamically created form fields

G

guido

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>
 
Y

Yuriy Solodkyy

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
 
G

guido

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?
 
Y

Yuriy Solodkyy

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 />
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top