Retrieving Values from dynamically created controls in a user control
From: Gary (ggraham_at_cecity.com)
Date: 06/02/04
- Next message: Simon Schmidt: "Custom Server Control Question"
- Previous message: Dave: "Pass values between two user controls?"
- Next in thread: Natty Gur: "Re: Retrieving Values from dynamically created controls in a user control"
- Reply: Natty Gur: "Re: Retrieving Values from dynamically created controls in a user control"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 2 Jun 2004 09:41:07 -0700
Hello,
I have a need to dynamically (programatically) create various different controls (TextBox, RadioButtonList, CheckBox, etc.) at page_load from within a User control - (.ascx). The reason is that the page content is dynamic and we are using a template .ascx page that loads the .ascx page that dynamically populates a panel within itself with the controls and labels.
I have no problem at all creating the controls. My problem is retrieving their values on post back.
Using Page.Request("controlID") produces nothing.
Is it possible to get the values back? If so, can someone tell me how I can get the control values?
Here is some proof of concept sample code from the page_loae event handler in the .ascx control.
This control has some tables, literals, and a panel named pnlFormBody on it.
If Not IsPostBack Then
litHeader.Text = "Dynamic Data Entry Form Example"
litTitle.Text = "Please answer the following questions"
litInstructions.Text = "This is a block of text that represents an instruction block on a " _
& "data entry form."
Dim lit As Literal
Dim tb As TextBox
Dim rbl As RadioButtonList
Dim cbl As CheckBoxList
Dim li As ListItem
Dim cb As CheckBox
Dim i As Integer
'begin table
lit = New Literal()
lit.Text = "<table>"
pnlFormBody.Controls.Add(lit)
'question 1
lit = New Literal()
lit.Text = "<tr><td width=""300"" valign=""top"">Please enter your napnlFormBody</td><td width=""400"">"
pnlFormBody.Controls.Add(lit)
tb = New TextBox()
tb.ID = "f1_q1_txt1"
pnlFormBody.Controls.Add(tb)
lit = New Literal()
lit.Text = "</td></tr>"
pnlFormBody.Controls.Add(lit)
'question 2
lit = New Literal()
lit.Text = "<tr width=""300"" bgcolor=""#eeeeee"" valign=""top""><td>Please select one</td><td>"
pnlFormBody.Controls.Add(lit)
rbl = New RadioButtonList()
rbl.ID = "f1_q2_rbl1"
For i = 1 To 4
li = New ListItem()
li.Text = "Selection " & i
li.Value = i
rbl.Items.Add(li)
Next
pnlFormBody.Controls.Add(rbl)
lit = New Literal()
lit.Text = "</td></tr>"
pnlFormBody.Controls.Add(lit)
'question 3
lit = New Literal()
lit.Text = "<tr width=""300"" valign=""top""><td>Please select one or more</td><td>"
pnlFormBody.Controls.Add(lit)
For i = 1 To 5
cb = New CheckBox()
cb.ID = "f1_q3_cbx" & i
cb.Text = "Choice " & i
pnlFormBody.Controls.Add(cb)
lit = New Literal()
lit.Text = "<br>"
pnlFormBody.Controls.Add(lit)
Next
cb = New CheckBox()
cb.ID = "f1_q3_cbxOther"
cb.Text = "Other"
pnlFormBody.Controls.Add(cb)
lit = New Literal()
lit.Text = " "
pnlFormBody.Controls.Add(lit)
tb = New TextBox()
tb.ID = "f1_q3_txtOther"
pnlFormBody.Controls.Add(tb)
lit = New Literal()
lit.Text = "</td></tr>"
pnlFormBody.Controls.Add(lit)
'question 4
lit = New Literal()
lit.Text = "<tr width=""300"" bgcolor=""#eeeeee"" valign=""top""><td>Will thios work?</td><td>"
pnlFormBody.Controls.Add(lit)
rbl = New RadioButtonList()
rbl.ID = "f1_q4_yn1"
rbl.RepeatDirection = RepeatDirection.Horizontal
li = New ListItem()
li.Text = "Yes"
li.Value = "Yes"
rbl.Items.Add(li)
li = New ListItem()
li.Text = "No"
li.Value = "No"
rbl.Items.Add(li)
pnlFormBody.Controls.Add(rbl)
lit = New Literal()
lit.Text = "</td></tr>"
pnlFormBody.Controls.Add(lit)
' close table
lit = New Literal()
lit.Text = "</table>"
pnlFormBody.Controls.Add(lit)
Else
' attempt to retrieve the data
Dim i As Integer
Dim q3Ans As String
Dim q1Ans As String = Page.Request("f1_q1_txt1")
Dim q2Ans As String = Page.Request("f1_q2_rbl1")
For i = 1 To 5
If Not Request("f1_q3_cbx" & i) Is Nothing Then
q3Ans += "Choice " & i & ", "
End If
Next
If Not Request("f1_q3_cbxOther") Is Nothing Then
q3Ans += "Other = "
q3Ans += Request("f1_q3_txtOther")
End If
Dim q4Ans As String = Request("f1_q4_yn1")
End If
This ascx control is loaded by the .aspx template page into a panel
on it.
I get nothing on the Request retrievals when the controls are created in
the user control and it is loaded into the panel on the .aspx page.
However it works just fine if the user control (.ascx) is eliminated and the
controls are loaded directly into the controls collection of the panel on the
.aspx page
Any ideas or help any one???
Thanks,
Gary
- Next message: Simon Schmidt: "Custom Server Control Question"
- Previous message: Dave: "Pass values between two user controls?"
- Next in thread: Natty Gur: "Re: Retrieving Values from dynamically created controls in a user control"
- Reply: Natty Gur: "Re: Retrieving Values from dynamically created controls in a user control"
- Messages sorted by: [ date ] [ thread ]