Page view counter

Repeater problem with duplicate control values

Last post 10-20-2008 12:16 PM by Lolli. 7 replies.

Sort Posts:

  • Repeater problem with duplicate control values

    10-20-2008, 10:46 AM
    • Loading...
    • mmriITPGM
    • Joined on 10-20-2008, 2:25 PM
    • Posts 4
    • Points 2

    I've searched and I can't find an answer to my problem.  I'm new to ASP .Net Repeaters, and I'm finding this confusing.

     I created a repeater and it is bound to a datasource that selects certains rows from a database.  The repeater contains several controls including a drop down list.  This list is populated at databinding as I need it to contain specific values, but also start with the correct selected index based on the value currently stored in the database.  Basically, I pull in a graduating year for a list of  students, and the drop down list has the possible levels - senior, junior, sophomore, etc.  Based on the graduating year in the database, the drop down list should display the corresponding level which could then be corrected by the user.  When I populate the controls in the repeater (including the DropDownList) using a function handling myRepeater.ItemDataBound, everything displays correctly, except the DDL populates with duplicate values.  What I mean is, the student name displays correctly, but within the DDL, the levels are displayed twice each - it shows Senior, Junior, Sophomore, ... and then Senior, Junior, Sophomore, ... again.

     The only workaround I came up with was to wait for the list to populate and then remove the last half of the DDL items based on the DDL.items.count.  It works, although I don't know why I am getting duplicates in the first place.

     I could live with that, but now when I try to read the controls upon the click of a button, everything is duplicated.  When I do a "FOR EACH item in myRepeater.Items" loop, I can find all of the controls using findcontrol.  To troubleshoot, I spit out the output values so I could read what would go back to the database.  Everything is duplicated.  It reads each item, writes out the values for each control in the itemtemplate, then duplicates again.  So, it shows the values (name, level, etc.) for student 1, student 2, student 3, ... and then repeats student 1, student 2, student 3, ...

     I am at a loss as to what could be causing this issue.  The repeater template displays the information properly, but the controls seem to be duplicating when I get their values.  I don't know if it is something that occurs at databinding, or on page postback, or what.  But on the page I see "John Doe" and the DDL has "senior" selected, and the next row has "Jane Doe" and "junior".  I submit the page, and the output values are:

    "John Doe", "senior"

    "Jane Doe", "senior"

    "John Doe", "senior"

    "Jane Doe", "senior"

    They only show up once in the repeater control, but the output duplicates.  It would be great if anyone could point me in the right direction to address this problem.  Part of the problem is that since items can also be added and not just updated, I can't afford to have duplicates inserted into the database.  Duplicate update statements wouldn't kill me, even though it's stupid to send the same update twice, but duplicate insertions are a problem.

     Thanks in advance

  • Re: Repeater problem with duplicate control values

    10-20-2008, 11:09 AM
    • Loading...
    • Lolli
    • Joined on 10-17-2006, 6:15 PM
    • Huntington Beach, CA
    • Posts 455
    • Points 2,309

    Can you post the repeater template code and your code behind?

    Lolli

    Don't forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: Repeater problem with duplicate control values

    10-20-2008, 11:31 AM
    • Loading...
    • mmriITPGM
    • Joined on 10-20-2008, 2:25 PM
    • Posts 4
    • Points 2

    I'm no expert, so bear with me Indifferent Some of this code may not be (is not) the best way, I'm sure, but it's been some trial and error as I try to learn some of this.  The "gradyear" in the database is the numeric year, while the DDL is supposed to display the text level (senior, etc.)  The last piece puts the values of each item into variables that I then output (eventually to UPDATE or INSERT for a database).

     

     <asp:Repeater id="rptActiveRoster" runat="server" OnItemDataBound="rptActiveRoster_OnItemDataBound">
                    <ItemTemplate>
                        <tr>
                            <td><asp:HiddenField ID="hdnRosternum" runat="server" /></td>
                            <td><asp:TextBox id="tbFirst" runat="server" class="oldTextbox" columns="25" maxlength="25"></asp:TextBox></td>
                            <td><asp:TextBox id="tbLast" runat="server" class="oldTextbox" columns="25" maxlength="25" Text=""></asp:TextBox></td>
                            <td><asp:TextBox id="tbMI" runat="server" class="oldTextbox" columns="5" maxlength="1" Text=""></asp:TextBox></td>
                            <td><asp:CheckBox id="cbActive" runat="server" checked="true" /></td>
                            <td><asp:DropDownList id="ddlyear" runat="server"></asp:DropDownList></td>
                        </tr>
                    </ItemTemplate>
                    <SeparatorTemplate />
    </asp:Repeater>

     

    Sub rptActiveRoster_OnItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptActiveRoster.ItemDataBound
            Dim rec As System.Data.DataRowView
            Dim tBox As TextBox
            Dim hdnField As HiddenField
            Dim dList As DropDownList
            Dim x As Integer
            rec = e.Item.DataItem
            hdnField = CType(e.Item.FindControl("hdnRosternum"), HiddenField)
            If Not hdnField Is Nothing Then hdnField.Value = rec("rosternum").ToString
            tBox = CType(e.Item.FindControl("tbFirst"), TextBox)
            If Not tBox Is Nothing Then tBox.Text = rec("pFirst").ToString
            tBox = CType(e.Item.FindControl("tbLast"), TextBox)
            If Not tBox Is Nothing Then tBox.Text = rec("pLast").ToString
            tBox = CType(e.Item.FindControl("tbMI"), TextBox)
            If Not tBox Is Nothing Then tBox.Text = rec("pMid").ToString

            dList = CType(e.Item.FindControl("ddlyear"), DropDownList)
            If Not dList Is Nothing Then
                dList.Items.Add(popDdlYear(0))
                dList.Items.Add(popDdlYear(1))
                dList.Items.Add(popDdlYear(2))
                dList.Items.Add(popDdlYear(3))
                dList.Items.Add(popDdlYear(4))
                dList.Items.Add(popDdlYear(5))
                dList.Items.Add(popDdlYear(6))
                'This likes to add two sets of items for some reason, remove below
                For x = dList.Items.Count To 8 Step -1
                    dList.Items.RemoveAt(x - 1)
                Next
                dList.Items.Item(0).Value = (Convert.ToInt32(EntryHandler.selYear) + 1).ToString
                dList.Items.Item(1).Value = (Convert.ToInt32(EntryHandler.selYear) + 2).ToString
                dList.Items.Item(2).Value = (Convert.ToInt32(EntryHandler.selYear) + 3).ToString
                dList.Items.Item(3).Value = (Convert.ToInt32(EntryHandler.selYear) + 4).ToString
                dList.Items.Item(4).Value = (Convert.ToInt32(EntryHandler.selYear) + 5).ToString
                dList.Items.Item(5).Value = (Convert.ToInt32(EntryHandler.selYear) + 6).ToString
                dList.Items.Item(6).Value = "9999"


                If rec("gradyear").ToString.Trim(String.Empty).Equals((Convert.ToInt32(EntryHandler.selYear) + 1).ToString) = True Then
                    dList.SelectedIndex = 0
                ElseIf rec("gradyear").ToString.Trim(String.Empty).Equals((Convert.ToInt32(EntryHandler.selYear) + 2).ToString) = True Then
                    dList.SelectedIndex = 1
                ElseIf rec("gradyear").ToString.Trim(String.Empty).Equals((Convert.ToInt32(EntryHandler.selYear) + 3).ToString) = True Then
                    dList.SelectedIndex = 2
                ElseIf rec("gradyear").ToString.Trim(String.Empty).Equals((Convert.ToInt32(EntryHandler.selYear) + 4).ToString) = True Then
                    dList.SelectedIndex = 3
                ElseIf rec("gradyear").ToString.Trim(String.Empty).Equals((Convert.ToInt32(EntryHandler.selYear) + 5).ToString) = True Then
                    dList.SelectedIndex = 4
                ElseIf rec("gradyear").ToString.Trim(String.Empty).Equals((Convert.ToInt32(EntryHandler.selYear) + 6).ToString) = True Then
                    dList.SelectedIndex = 5
                Else
                    dList.SelectedIndex = 6
                End If

            End If


        End Sub

        Public Function popDdlYear(ByVal listPos As Integer) As String
            Select Case listPos
                Case 0
                    Return "Senior"
                Case 1
                    Return "Junior"
                Case 2
                    Return "Sophomore"
                Case 3
                    Return "Freshman"
                Case 4
                    Return "8th"
                Case 5
                    Return "7th"
                Case Else
                    Return "Unknown"
            End Select

        End Function

        Protected Sub btnActiveRoster_OnClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnActiveRoster.Click
            'Loop through Repeater
            Dim rARItem As Object
            Dim tBox As TextBox
            Dim hdnField As HiddenField
            Dim dList As DropDownList
            Dim cBox As CheckBox
            Dim hdnRosternum As String = String.Empty
            Dim tbFirst As String = String.Empty
            Dim tblast As String = String.Empty
            Dim tbMI As String = String.Empty
            Dim ddlyear As String = String.Empty
            Dim cbActive As Boolean = True
            Dim x As Integer
            For Each rARItem In rptActiveRoster.Items
                hdnField = CType(rARItem.FindControl("hdnRosternum"), HiddenField)
                If Not hdnField Is Nothing Then hdnRosternum = hdnField.Value.ToString
                tBox = CType(rARItem.FindControl("tbFirst"), TextBox)
                If Not tBox Is Nothing Then tbFirst = tBox.Text.ToString
                tBox = CType(rARItem.FindControl("tbLast"), TextBox)
                If Not tBox Is Nothing Then tblast = tBox.Text.ToString
                tBox = CType(rARItem.FindControl("tbMI"), TextBox)
                If Not tBox Is Nothing Then tbMI = tBox.Text.ToString
                dList = CType(rARItem.FindControl("ddlyear"), DropDownList)
                If Not dList Is Nothing Then ddlyear = dList.SelectedValue.ToString
                cBox = CType(rARItem.findcontrol("cbActive"), CheckBox)
                If Not cBox Is Nothing Then cbActive = cBox.Checked.ToString
            Next

        End Sub

  • Re: Repeater problem with duplicate control values

    10-20-2008, 11:44 AM
    Answer
    • Loading...
    • Lolli
    • Joined on 10-17-2006, 6:15 PM
    • Huntington Beach, CA
    • Posts 455
    • Points 2,309

    The first thing you need to do is clear the list prior to populating it. I'm no vb coder but it should look like this:

    dList = CType(e.Item.FindControl("ddlyear"), DropDownList)
    If Not dList Is Nothing Then
        dList.Items.clear()

     

     

    Lolli

    Don't forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: Repeater problem with duplicate control values

    10-20-2008, 11:53 AM
    • Loading...
    • mmriITPGM
    • Joined on 10-20-2008, 2:25 PM
    • Posts 4
    • Points 2

     Thanks, that takes care of the DDL problem.  But the duplicate controls problem still exists.  When I click the button, the duplicate names still appear. 

     I did a quick            

    x += 1
    Response.Write(String.Concat(x.ToString, " - ", hdnRosternum, ": ", cbActive, ": ", ddlyear))
    Response.Write("<hr>")

    to the  btnActiveRoster_OnClick sub (just before the NEXT statement), and the output still duplicates (names removed to protect the innocent)

     

    1 - 3047: True: 2013


    2 - 2599: True: 2009
    3 - 3048: True: 2009
    4 - 2004: True: 2009
    5 - 2493: True: 2009
    6 - 3380: True: 2011
    7 - 3046: True: 2010
    8 - 3049: True: 2009
    9 - 3050: True: 2009
    10 - 2962: True: 2010
    11 - 3381: True: 2011
    1 - 3047: True: 2013
    2 - 2599: True: 2009
    3 - 3048: True: 2009
    4 - 2004: True: 2009
    5 - 2493: True: 2009
    6 - 3380: True: 2011
    7 - 3046: True: 2010
    8 - 3049: True: 2009
    9 - 3050: True: 2009
    10 - 2962: True: 2010
    11 - 3381: True: 2011

     

     


     

  • Re: Repeater problem with duplicate control values

    10-20-2008, 12:08 PM
    Answer
    • Loading...
    • Lolli
    • Joined on 10-17-2006, 6:15 PM
    • Huntington Beach, CA
    • Posts 455
    • Points 2,309

    It looks to me like your btnActiveRoster_OnClick is called twice, not that your repeater is wrong....

    Lolli

    Don't forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: Repeater problem with duplicate control values

    10-20-2008, 12:12 PM
    • Loading...
    • mmriITPGM
    • Joined on 10-20-2008, 2:25 PM
    • Posts 4
    • Points 2

     I think you got it.  I'm an idiot.  I didn't realize I didn't have to call the onclick event within the asp:button.  I removed that and now when I click it, it only calls once and appears to be correct.

    I knew it had to be something I screwed up.  Thanks for your help.  Sometimes it just takes someone else pointing out the obvious to me Stick out tongue

  • Re: Repeater problem with duplicate control values

    10-20-2008, 12:16 PM
    • Loading...
    • Lolli
    • Joined on 10-17-2006, 6:15 PM
    • Huntington Beach, CA
    • Posts 455
    • Points 2,309

    Happens to all of us :)

    Lolli

    Don't forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
Page 1 of 1 (8 items)