locked
Order of the loads RRS feed

  • Question

  • User545494261 posted

    Hi all:

    I have a dropdownlist and a repeater.

    I make some operations in the dropdownlist.databound event and repeater.itemdatabound

    I need that the  load of the repeater takes place before the dropdownist because in this event I reference an element of the repeater.

            If DirectCast(rptimporte.Items(0).FindControl("litimporte"), Literal).Text = 0 Then
                s.Items(4).Enabled = False
            Else
                s.SelectedValue = 6
            End If
    

    I see that in some cases in the aspx pages (I have the same dropdownlist and repeter in various pages) the repeater is loaded before the dropdownlist, and in other cases the dropdownlist is loaded before the repeater, and in this case I get an error

    El índice estaba fuera del intervalo. Debe ser un valor no negativo e inferior al tamaño de la colección. Nombre del parámetro: index

    This is because the repeater has not elements.

    What the controls load depend of? Can I manage it?

    Thanks in advance.

    Sunday, July 26, 2020 9:08 AM

Answers

  • User1535942433 posted

    Hi volar.2016,

    What the controls load depend of? Can I manage it?

    As far as I think,you couldn't manage the order the order of the loads.OnItemDataBound is modified after it is bound to an item in the Repeater control but before it is rendered on the page. And DataBound is raised at the end of the data binding operation in the data bound control.The error is not relation of the order.

    This is because the repeater has not elements.

    The error is when you are excuting the onitemdatanound event,the item didn't databound.

    Accroding to your codes,I have create a demo.When the repeater index=0.then you will find the Literal control.You could bind the data of dropdownlist in the codes.

    More details,you could refer to below codes:

           <div>
                <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
                    <HeaderTemplate>
                        <asp:Label runat="server" Text="text"></asp:Label>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Literal ID="Literal1" runat="server" Text='<%# Eval("Year") %>'></asp:Literal>
                    </ItemTemplate>
                </asp:Repeater>
            </div>
            <asp:DropDownList ID="DropDownList1" runat="server" OnDataBound="DropDownList1_DataBound" AutoPostBack="false"></asp:DropDownList>
    

    Code-behind:

      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Me.IsPostBack Then
                Me.BindDrop()
                Me.BindRepeater()
    
            End If
        End Sub
        Private Sub BindRepeater()
            Dim constr As String = ConfigurationManager.ConnectionStrings("aspnet-TestApplicationWithDatabase-20190820030542").ConnectionString
            Using con As SqlConnection = New SqlConnection(constr)
                Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Sum", con)
                    Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
                        Dim dt As DataTable = New DataTable()
                        sda.Fill(dt)
                        Repeater1.DataSource = dt
                        Repeater1.DataBind()
                    End Using
                End Using
            End Using
        End Sub
        Private Sub BindDrop()
            Dim constr As String = ConfigurationManager.ConnectionStrings("aspnet-TestApplicationWithDatabase-20190820030542").ConnectionString
            Using con As SqlConnection = New SqlConnection(constr)
                Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Sum", con)
                    Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
                        Dim dt As DataTable = New DataTable()
                        sda.Fill(dt)
                        DropDownList1.DataSource = dt
    
                        DropDownList1.DataTextField = "Id"
                        DropDownList1.DataValueField = "Id"
    
                        DropDownList1.DataBind()
                    End Using
                End Using
            End Using
        End Sub
        Protected Sub DropDownList1_DataBound(sender As Object, e As EventArgs)
            If (DropDownList1.Items.Count > 0) Then
                For Each li As ListItem In DropDownList1.Items
                    li.Text = Server.HtmlDecode(li.Text)
    
                Next
            End If
    
        End Sub
    
        Protected Sub Repeater1_ItemDataBound(sender As Object, e As RepeaterItemEventArgs)
            If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
                If e.Item.ItemIndex = 0 Then
    
                    If DirectCast(e.Item.FindControl("Literal1"), Literal).Text = 0 Then
    
                        'Label1.Text = "1"
                        DropDownList1.Items(2).Enabled = False
                    Else
                        DropDownList1.SelectedValue = 3
                    End If
    
    
                End If
            End If
    
        End Sub

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 27, 2020 3:44 AM