Link to home
Start Free TrialLog in
Avatar of Rouchie
RouchieFlag for United Kingdom of Great Britain and Northern Ireland

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("answerText").ToString
myList.DataValueField = myDataTable.Columns("answerID").ToString
myList.DataBind()
Avatar of Nandakumar Sakthivel
Nandakumar Sakthivel
Flag of United States of America image

Hai,

Dim myList As ListControl
            If True Then
                myList = New RadioButtonList
            Else
                myList = New CheckBoxList
            End If
            Dim myForm As HtmlForm
            myForm = CType(Me.FindControl("Form1"), HtmlForm)
            myList.ID = "myList"
            myForm.Controls.Add(myList)
myList.DataSource = myDataTable
myList.DataTextField = myDataTable.Columns("answerText").ToString
myList.DataValueField = myDataTable.Columns("answerID").ToString
myList.DataBind()

i tried with the code u have given.. i added the lines
 Dim myForm As HtmlForm
            myForm = CType(Me.FindControl("Form1"), HtmlForm)
            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
Avatar of Rouchie

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
Avatar of Nandakumar Sakthivel
Nandakumar Sakthivel
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rouchie

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