Dropdownlist in a repeater, selected index changed not working

CustomX picture CustomX · May 19, 2011 · Viewed 15.8k times · Source

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.

Answer

Tim B James picture Tim B James · May 22, 2011

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