Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Distribution of items from an ArrayList to a particular instance of a Repeater

Posted on 2006-05-26
7
Medium Priority
?
364 Views
Last Modified: 2011-10-03
Hi,

I'm trying to populate a RadioButtonList (each RadioButton is a possible multiple-choice answer) that is within a Repeater (each being a question).  The RBL is being populated by an ArrayList.  The format should look something like this:

Repeater1 (i.e. Question 1)
  RB1  (i.e. multiple-choice 1)
  RB2  (m-c 2)  etc...
  RB3
  RB4
  RB5

Repeater2
  RB1
  RB2
  RB3
  RB4
  RB5

Right now, I have all the answers for ALL the questions showing up under EACH question:

Repeater1
   RB1
   RB2
   RB3
   RB4
   RB5
   RB1
   RB2
   RB3
   RB4
   RB5

Repeater2
   RB1
   RB2
   RB3
   RB4
   RB5
   RB1
   RB2
   RB3
   RB4
   RB5

How can I take specific strings from the ArrayList and populate specific instances of a repeater with them?  Here's what my ItemDataBound code for the Repeater looks like:

    Public Sub rptQuestions_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptQuestions.ItemDataBound
        Dim lt As ListItemType = e.Item.ItemType
        If lt = ListItemType.Item OrElse lt = ListItemType.AlternatingItem Then
            Dim strAnswers As String = CType(e.Item.DataItem, String)
            If Not strAnswers Is Nothing Then
                Dim rblAnswers As RadioButtonList = CType(e.Item.FindControl("rblAnswers"), RadioButtonList)
                If Not rblAnswers Is Nothing Then
                    Dim alAnswers As ArrayList
                    alAnswers = DA.PopulateAnswers(CInt(Request.QueryString(0)))
                    rblAnswers.DataSource = alAnswers
                    rblAnswers.DataBind()
                End If
            End If
        End If
    End Sub

Hope this isn't too confusing.
Jens
0
Comment
Question by:tmccrank
  • 4
  • 3
7 Comments
 
LVL 12

Expert Comment

by:sachitjain
ID: 16774007
I think checkboxlist control is the better option if u want to have multiple choice answers.

Scenario should be like, take a repeater control with a label and checkboxlist (label to display question, checkboxlist to display answers) and bind that repeater with the datasource(table populated with questions and possible options for those questions).

I don't think u need to have a separate repeater control for each question.

If my understanding is not clear about ur question then plz correct me.

Regards
Sachit.
0
 

Author Comment

by:tmccrank
ID: 16784775
Hi Sachit,

The .aspx page code looks like this:

--------------------------------

      <body MS_POSITIONING="GridLayout">
            <form id="Form1" method="post" runat="server">
                  <asp:repeater id="rptQuestions" runat="server" OnItemDataBound="rptQuestions_ItemDataBound">
                        <HeaderTemplate>
                              <ol>
                        </HeaderTemplate>
                        <ItemTemplate>
                              <li>
                                    <%# Container.DataItem %>
                              </li>
                              <asp:RadioButtonList ID="rblAnswers" Runat="server"></asp:RadioButtonList>
                        </ItemTemplate>
                        <FooterTemplate>
                              </ol>
                        </FooterTemplate>
                  </asp:repeater>
            </form>
      </body>

--------------------------------

As you can see, I have a RadioButtonList nested in a Repeater control.  Each control gets its data from separate tables: one table for the questions (the repeater), and one for the list of possible answers (the RBL).  A DataReader is used to read the data into separate ArrayLists which then are the data sources for each control.  I want the RBL control (not the CheckBoxList) since the user should be able to choose only one of five answers for each question.

So, my main question is, how do I populate each question with the list of answers that are relevant to only that question?

Here's the Page_Load event code:

--------------------------------

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'retrieve ArrayList
        Dim alQuestions As ArrayList
        alQuestions = DA.PopulateQuestions(CInt(Request.QueryString(0)))

        'bind the questions repeater to the alQuestions ArrayList
        rptQuestions.DataSource = alQuestions
        rptQuestions.DataBind()

    End Sub

--------------------------------

And here is the ItemDataBound code for populating the RadioButtonList:

--------------------------------

    Public Sub rptQuestions_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptQuestions.ItemDataBound
        Dim lt As ListItemType = e.Item.ItemType
        If lt = ListItemType.Item OrElse lt = ListItemType.AlternatingItem Then
            Dim strAnswers As String = CType(e.Item.DataItem, String)
            If Not strAnswers Is Nothing Then
                Dim rblAnswers As RadioButtonList = CType(e.Item.FindControl("rblAnswers"), RadioButtonList)
                If Not rblAnswers Is Nothing Then
                    Dim alAnswers As ArrayList
                    alAnswers = DA.PopulateAnswers(CInt(Request.QueryString(0)))
                    rblAnswers.DataSource = alAnswers
                    rblAnswers.DataBind()
                End If
            End If
        End If
    End Sub

--------------------------------

Thanks
Jens
0
 

Author Comment

by:tmccrank
ID: 16785228
Maybe this would be easier if I pulled down the data from the two SQL tables into one ArrayList instead of two.

If I do that I need to figure out how to populate the Questions Repeater AND the Answer RBL (nested in the Repeater) from the ArrayList...
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 12

Expert Comment

by:sachitjain
ID: 16786940
What i m noticing here is that u r creating separate arraylists for binding questions.
In ItemDataBound, plz verify that DA.PopulateAnswers method is returning answers for respective question because ItemDataBound event would execute once for each question.
Say one question is there with quesitionNo as 1 and 1A, 1B, 1C, 1D and 1E are possible options for question 1 then when ItemDataBound is getting executed for question 1, ur method DA.PopulateAnswers should set the arraylist with records 1A to 1E only.

Regards
Sachit.
0
 

Author Comment

by:tmccrank
ID: 16793329
Sachit,
Yes, DA.PopulateAnswers is returning an ArrayList of answers for each question.  Here's the code:

----------------------------

    Public Function PopulateAnswers(ByVal QuestID As Integer) As ArrayList

        Dim alPopAnswers As New ArrayList
        Dim strPopAnswers As String = ("SELECT a.Answers, a.QuestionsID, q.QuestionsID, q.ModuleID, m.ModuleID ")
        strPopAnswers = strPopAnswers & ("FROM Answers a INNER JOIN Questions q ")
        strPopAnswers = strPopAnswers & ("ON a.QuestionsID = q.QuestionsID ")
        strPopAnswers = strPopAnswers & ("INNER JOIN Modules m ON q.ModuleID = m.ModuleID WHERE m.ModuleID='") & QuestID + 1 & ("'")
        Dim drPopAnswers As SqlDataReader
        Dim commPopAnswers As New SqlCommand(strPopAnswers, conNEM)

        Try
            'open connection
            conNEM.Open()

            'execute reader, then close connection
            drPopAnswers = commPopAnswers.ExecuteReader(CommandBehavior.CloseConnection)

            'iterate through result set, add to ArrayList
            While drPopAnswers.Read()
                alPopAnswers.Add(drPopAnswers("Answers"))
            End While

        Catch ex As Exception
            ex.ToString()

        Finally
            'close datareader
            drPopAnswers.Close()

        End Try

        Return alPopAnswers

    End Function

-----------------------------------

To use your analogy, if I have 3 Questions, what I'm ending up with is:

Question 1: 1A, 1B, 1C, 1D, 1E, 2A, 2B, 2C, 2D, 2E, 3A, 3B, 3C, 3D, 3E
Question 2: 1A, 1B, 1C, 1D, 1E, 2A, 2B, 2C, 2D, 2E, 3A, 3B, 3C, 3D, 3E
Question 3: 1A, 1B, 1C, 1D, 1E, 2A, 2B, 2C, 2D, 2E, 3A, 3B, 3C, 3D, 3E

So the answers for all 3 questions are showing up for each question.  Of course it should be like this:

Question 1: 1A, 1B, 1C, 1D, 1E
Question 2: 2A, 2B, 2C, 2D, 2E
Question 3: 3A, 3B, 3C, 3D, 3E

Get it?  Do you think it would be better if I have just one ArrayList (with the questions and answers from the SQL DB tables) and somehow iterate through it to populate the Questions Repeater and the Answers RadioButtonList?  If so, how could that be done?

If I stay with two ArrayLists, I somehow need to loop through the Answers ArrayList where the loop recognizes which Question index it's on.  The problem here is that the Questions are in another ArrayList.  That's why I'm thinking it might be better to have one ArrayList instead of two.

Thanks and regards,
Jens
0
 
LVL 12

Accepted Solution

by:
sachitjain earned 2000 total points
ID: 16796106
You could do one thing here

Populate a datatable(through dataset) rather than datareader with records from database.

Once it is done, u could create a dataview over datatable and then use RowFilter property of dataview to filter out answers for each question.
We could specify RowFilter property value as simple clause (like where of sql).
e.g QuestionsID = 2

Regards
Sachit Jain.
0
 

Author Comment

by:tmccrank
ID: 16817982
Sachit,
Thanks for your help.  I looks like using a DataTable is the way to go, I was hoping to stay with using ArrayLists since they're such light-weight objects compared to DataSets.  There seems to be a growing number of people out there who are actively advising against using DataSets under ANY circumstances!

I'm not sure yet how to use the RowFilter property, but that's another problem that I'll try to figure out.

Jens
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question