Radiobuttonlist in Datalist

Last post 11-20-2007 8:29 PM by pinkyy. 2 replies.

Sort Posts:

  • Radiobuttonlist in Datalist

    11-20-2007, 4:18 AM
    • Member
      4 point Member
    • pinkyy
    • Member since 10-04-2007, 9:14 PM
    • Singapore
    • Posts 24

    Hi

    I have a datalist which contains radiobuttonlist..
    As i want to make it in a format like a test paper..

    Example:

    1. What is the colour of ?

    • White
    • Black
    • Green
    • Blue

    The QuestionID is hidden (visible = false) in the item template too..
    I'm able to find the QuestionID by FindControl()

    But due to the program needs.. The Radiobuttonlist should not be retrieve with the Questions due to database table.
    So i have search out the QuestionID and based on the QuestionID, I got the answers and answerID for the particular question.

    I did...

    For i = 0 To DataList1.Items.Count - 1

    Dim con_rbn As RadioButtonList = CType(DataList1.Items.Item(i).FindControl("rbn_list"), RadioButtonList)

    DS_Lecturer = New Data.DataSet
    DS_Lecturer = Tier.getAnsForMCQ(CType(DataList1.Items.Item(i).FindControl("questionIDLabel"), Label).Text)

    con_rbn.DataSource = DS_Lecturer
    con_rbn.DataTextField = "mcqAnswer"
    con_rbn.DataValueField = "mcqID"
    con_rbn.DataBind()

    Next

    But the radiobutton did not show the information where the datasource contains the information needed.

    Thanks in advance for your help !
    FYI: For the Questions, i used SQLDataSource, where for the radiobuttonlist i coded it as you can see.

    Happy is the person who can endure both the highest and lowest fortunes.
  • Re: Radiobuttonlist in Datalist

    11-20-2007, 4:19 PM
    Answer
    • Star
      12,579 point Star
    • codeasp
    • Member since 06-16-2004, 5:19 PM
    • Posts 2,201

    1.  Instead of hiding a label, I would suggest you use the DataKeys property of the DataList.

    2.  Your code is partially correct.  It is just a matter, where do you add this code.  Appropriate place would be the ItemDataBound event of the DataList. 

    Sub Item_Bound(sender As Object, e As DataListItemEventArgs)
    
             If e.Item.ItemType = ListItemType.Item Or _
                 e.Item.ItemType = ListItemType.AlternatingItem Then
    
    
                ' Retrieve the Label control in the current DataListItem.
                Dim QuestionLabel As Label = _
                    CType(e.Item.FindControl("QuestionLabel"), Label)
    
                'Retrieve the QuestionID
                Dim QuestionID as Integer = Cint(QuestionLabel.Text)
    
               ' Retrieve the RadioButtonList
               Dim rb As RadioButtonList = CType(e.Item.FindControl("rbn_list"), RadioButtonList)
               
               rb.DataSource = Tier.getAnsForMCQ(QuestionID)
               rb.DataBind()
             End If
    
          End Sub
    
     
  • Re: Radiobuttonlist in Datalist

    11-20-2007, 8:29 PM
    • Member
      4 point Member
    • pinkyy
    • Member since 10-04-2007, 9:14 PM
    • Singapore
    • Posts 24

    Just to add on in case others need to reference...

    To be added after DataSource else it will return System.Data.DataRowView
    for all field...

    Cheers !! Yes

    rb.DataTextField = "mcqAnswer"
    rb.DataValueField = "mcqID"

    Happy is the person who can endure both the highest and lowest fortunes.
Page 1 of 1 (3 items)