Question

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.

Was it helpful?

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

OTHER TIPS

Text is probably the default property, but I'd still specify it:

lbldummy.Text = ddlSizes.value

but for this, you really don't need to do a postback, you can accomplish this through Javascript as well. doing something like this:

<asp:DropDownList ID="ddlSizes" runat="server" onchange="return ddlSizes_change(this);" DataSourceID="objdsSizes"  DataTextField="SizeName" DataValueField="SizeID" />


function ddlSizes_change(dropdown)
{
     document.getElementById('<%= lbldummy.ClientID %>').innerHTML = 
         dropdown.options[myindex].value

    return true;
}

Here's an example (C# but easily adaptable to VB.NET). Notice how inside the DdlSizes_SelectedIndexChanged I use FindControl to find the corresponding label:

<%@ Page Language="C#" %>
<script type="text/c#" runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            rep.DataSource = Enumerable.Range(1, 5);
            rep.DataBind();
        }
    }

    protected void DdlSizes_SelectedIndexChanged(object sender, EventArgs e)
    {
        var ddl = (DropDownList)sender;
        var lbl = (Label)ddl.FindControl("lbldummy");
        lbl.Text = ddl.SelectedValue;
    }
</script>
<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="Form1" runat="server">
        <asp:Repeater ID="rep" runat="server">
            <ItemTemplate>
                <asp:DropDownList ID="ddlSizes" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DdlSizes_SelectedIndexChanged">
                    <asp:ListItem Value="1" Text="item 1" />
                    <asp:ListItem Value="2" Text="item 2" />
                    <asp:ListItem Value="3" Text="item 3" />
                </asp:DropDownList>
                <asp:Label ID="lbldummy" runat="server" />
            </ItemTemplate>
        </asp:Repeater>
    </form>
</body>
</html>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top