Programatically created controls in an ascx user control misbehaving

G

Gary Graham

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 The
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 Litera
Dim tb As TextBo
Dim rbl As RadioButtonLis
Dim cbl As CheckBoxLis
Dim li As ListIte
Dim cb As CheckBo
Dim i As Intege

'begin tabl
lit = New Literal(
lit.Text = "<table>
pnlFormBody.Controls.Add(lit

'question
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
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
li = New ListItem(
li.Text = "Selection " &
li.Value =
rbl.Items.Add(li
Nex
pnlFormBody.Controls.Add(rbl
lit = New Literal(
lit.Text = "</td></tr>
pnlFormBody.Controls.Add(lit

'question
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
cb = New CheckBox(
cb.ID = "f1_q3_cbx" &
cb.Text = "Choice " &
pnlFormBody.Controls.Add(cb
lit = New Literal(
lit.Text = "<br>
pnlFormBody.Controls.Add(lit
Nex
cb = New CheckBox(
cb.ID = "f1_q3_cbxOther
cb.Text = "Other
pnlFormBody.Controls.Add(cb
lit = New Literal(
lit.Text = "&nbsp;
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
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.Horizonta
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 tabl
lit = New Literal(
lit.Text = "</table>
pnlFormBody.Controls.Add(lit


Els

' attempt to retrieve the dat
Dim i As Intege
Dim q3Ans As Strin
Dim q1Ans As String = Page.Request("f1_q1_txt1"
Dim q2Ans As String = Page.Request("f1_q2_rbl1"
For i = 1 To
If Not Request("f1_q3_cbx" & i) Is Nothing The
q3Ans += "Choice " & i & ",
End I
Nex
If Not Request("f1_q3_cbxOther") Is Nothing The
q3Ans += "Other =
q3Ans += Request("f1_q3_txtOther"
End If

Dim q4Ans As String = Request("f1_q4_yn1")

End If


This control is loaded by the .aspx template page into a panel using the LoadControl method


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
 
J

John Saunders

Gary Graham said:
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?

You need to create the controls on PostBack as well, and in the exact same
order you did on the initial request. You'll then be able to retrieve the
values from the controls themselves.
 
G

Gary Graham

John

Thanks for the suggestion it does indeed work. Also, I discovered that the problem was that the ascx control implements INamingContainer which creates a unique name for each control within the user control. In this case it was using the default which is "_ctrl0:" so that the control named "f1_q1_txt1" is actualy set up with id= '_ctrl0_f1_q1_txt1' and name='_ctrl0:f1_q1_txt1'. In order to retrieve the value on the request you need to use
value = Request("_ctrl0:f1_q1_txt1")

This leaves two options: find the unique tag in the Request.Form collection and prepend it or insure that you know the tag ahead of time which can easily be done by adding a constructor to the .ascx control like

Public Sub New(

Me.ID = "GRG" ' just as an exampl

End Su

and then using that ID in the code as in

If Not Request(Me.ID & ":f1_q1_txt1") Is Nothing The

q1Ans = Request(Me.ID & ":f1_q1_txt1")

End I

or something similar - the If block in this case is probably overkill but not if multiple answers were possible say from a checkbox group

Actually a combination of your suggestion and this method really locks it down

Thanks for your reply

Best wishes

Gary
 
J

John Saunders

Gary Graham said:
John,

Thanks for the suggestion it does indeed work. Also, I discovered that
the problem was that the ascx control implements INamingContainer which
creates a unique name for each control within the user control. In this
case it was using the default which is "_ctrl0:" so that the control named
"f1_q1_txt1" is actualy set up with id= '_ctrl0_f1_q1_txt1' and
name='_ctrl0:f1_q1_txt1'. In order to retrieve the value on the request
you need to use:
value = Request("_ctrl0:f1_q1_txt1") .

This leaves two options: find the unique tag in the Request.Form
collection and prepend it or insure that you know the tag ahead of time
which can easily be done by adding a constructor to the .ascx control like:
Public Sub New()

Me.ID = "GRG" ' just as an example

End Sub


and then using that ID in the code as in:

If Not Request(Me.ID & ":f1_q1_txt1") Is Nothing Then

q1Ans = Request(Me.ID & ":f1_q1_txt1")

End If

or something similar - the If block in this case is probably overkill but
not if multiple answers were possible say from a checkbox group.
Actually a combination of your suggestion and this method really locks it
down.

I'd still recommend that you not use Request.Form, but instead use the
properties of the control.
 

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,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top