By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,169 Members | 1,661 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,169 IT Pros & Developers. It's quick & easy.

Iterate through dynamically created form fields

P: n/a
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>

Jun 15 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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>

Jun 15 '07 #2

P: n/a
On 15 Jun, 15:31, Yuriy Solodkyy <y.dot.solod...@gmail.comwrote:
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?

Jun 16 '07 #3

P: n/a
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 <y.dot.solod...@gmail.comwrote:
>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?

Jun 16 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.