Rouchie
asked on
Read posted values from a dynamically created listControl
I have a listControl that is created dynamically in code-behind. Depending on the number of answers required, the control is set to be either a checkboxlist or radio button list, and then this control is added to a placeholder for display on-screen.
My question is how do I retrieve the posted values when the user submits the form? I'm getting an 'object reference not set' error because obviously this form element is not hard coded.
My code to create the list object is below. Any help on retrieving these values would be very welcome.
Dim myList As ListControl
If answersRequired = 1 Then
myList = New RadioButtonList
Else
myList = New CheckBoxList
End If
myPlaceHolder.Controls.Add (myList)
myList.DataSource = myDataTable
myList.DataTextField = myDataTable.Columns("answe rText").To String
myList.DataValueField = myDataTable.Columns("answe rID").ToSt ring
myList.DataBind()
My question is how do I retrieve the posted values when the user submits the form? I'm getting an 'object reference not set' error because obviously this form element is not hard coded.
My code to create the list object is below. Any help on retrieving these values would be very welcome.
Dim myList As ListControl
If answersRequired = 1 Then
myList = New RadioButtonList
Else
myList = New CheckBoxList
End If
myPlaceHolder.Controls.Add
myList.DataSource = myDataTable
myList.DataTextField = myDataTable.Columns("answe
myList.DataValueField = myDataTable.Columns("answe
myList.DataBind()
ASKER
For some reason when I try this the request.form value is blank. If possible, could you please post the code for a full working example so that I can break it down into pieces and try it myself?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I had to change the example you posted to cycle through the controls to find the values of this list. Sadly this has developed into a further question of which I would be very grateful of any assistance. https://www.experts-exchange.com/questions/21911175/Trying-to-place-a-ListControl-in-page-init.html
Dim myList As ListControl
If True Then
myList = New RadioButtonList
Else
myList = New CheckBoxList
End If
Dim myForm As HtmlForm
myForm = CType(Me.FindControl("Form
myList.ID = "myList"
myForm.Controls.Add(myList
myList.DataSource = myDataTable
myList.DataTextField = myDataTable.Columns("answe
myList.DataValueField = myDataTable.Columns("answe
myList.DataBind()
i tried with the code u have given.. i added the lines
Dim myForm As HtmlForm
myForm = CType(Me.FindControl("Form
myList.ID = "myList"
myForm.Controls.Add(myList
-- now u can access the value user has selected by using request.form
request.Form("myList")
Regards
Nanda