Search code examples
asp.netvb.netdrop-down-menurepeaterselectedindexchanged

Dropdownlist in a repeater, selected index changed not working


I have a repeater with a dropdownlist in it. When a user changes its index, I would like a label to change its value. (the ddlSizes values come from a MySQL DB)

Sizes.aspx

<asp:DropDownList ID="ddlSizes" runat="server" AutoPostBack="True" DataSourceID="objdsSizes"  DataTextField="SizeName" DataValueField="SizeID" />

<asp:Label ID="lbldummy" runat="server" Text=""></asp:Label>

Sizes.aspx.vb

Protected Sub ddlSizes_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlSizes.SelectedIndexChanged
    lbldummy = ddlSizes.value
End Sub

But the ddlSizes.SelectedIndexChanged isn't recognized. So the value of lbldummy won't change.

Any suggestions? Thank you.


Solution

  • You will want to create the handler for the DropDownList, within this you need to have code which will convert the sender into a DropDownList then get the parent control and convert it into the RepeaterItem. From this you can then reference any other controls within the RepeaterItem

    Public Sub ddlSizes_SelectedIndexChanged(sender As Object, e As EventArgs)
        Dim ddlSizes As DropDownList = DirectCast(sender, DropDownList)
        Dim ri As RepeaterItem = DirectCast(ddlSizes.Parent, RepeaterItem)
        Dim lbldummy As Label = DirectCast(ri.FindControl("lbldummy"), Label)
        lbldummy.Text = ddlSizes.SelectedValue
    End Sub
    

    Then on your ddlSizes DropDownList add OnSelectedIndexChanged="ddlSizes_SelectedIndexChanged" and make sure it has AutoPostBack="True" set