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?
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.
volar.2016
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.
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
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
6 Points
25 Posts
Order of the loads
Jul 26, 2020 09:08 AM|volar.2016|LINK
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.
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
This is because the repeater has not elements.
What the controls load depend of? Can I manage it?
Thanks in advance.
Contributor
3730 Points
1417 Posts
Re: Order of the loads
Jul 27, 2020 03:44 AM|yij sun|LINK
Hi volar.2016,
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.
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:
Code-behind:
Best regards,
Yijing Sun