2

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.

CustomX
  • 9,948
  • 30
  • 85
  • 115
  • 3
    I believe your DropDownList control should look like this: – Tchami May 19 '11 at 16:38
  • I tried did it your way Tchami, but I still get blue lines under ddlSizes.SelectedIndexChanged. Something about requiring a WithEvent? – CustomX May 19 '11 at 16:42

3 Answers3

7

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

Community
  • 1
  • 1
Tim B James
  • 20,084
  • 4
  • 73
  • 103
  • I have a situation that is similar to this. My dropdown list chooses a value then the repeater needs to be tied to it to further filter a gridview. I tried the code you wrote here and get the error `Unable to cast object of type 'System.Web.UI.WebControls.ContentPlaceHolder' to type 'System.Web.UI.WebControls.RepeaterItem'.` Please check here for my question....http://stackoverflow.com/q/7935135/908153 – Jamie Oct 31 '11 at 14:22
  • 1
    @tim-b-james Should that be Label not Llabel on line 4? – JsonStatham Aug 28 '14 at 15:20
  • @SelectDistinct indeed it should be. Go ahead an edit if you want :) – Tim B James Aug 28 '14 at 15:27
1

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;
}
stephenbayer
  • 12,373
  • 15
  • 63
  • 98
  • Joe Tuskan found that the OnSelectedIndexChanged was not set on the options, which also can be the case, I was thinking the Auto Event Wireup stuff was set. – stephenbayer May 19 '11 at 16:42
  • Well PostBack would be easier cause I'll be working with AJAX – CustomX May 19 '11 at 16:44
  • that's cool, it seemed trivial enough not to have to deal with the overhead of a complete postback. As a note on the AJAX with .NET, understand that all the code in the page load and other events will run with each post. I didn't know that the first time I used it. – stephenbayer May 19 '11 at 17:14
1

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>
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • But that's in C#? I'd prefer to do it just in asp.net – CustomX May 19 '11 at 16:45
  • @Tom, yes it's C# and it's a full working example. Adapt it to your needs. It's easy. Just take the code I have in my `DdlSizes_SelectedIndexChanged` using the FindControl method and put it in your `DdlSizes_SelectedIndexChanged` method after translating the 3 lines of code to VB.NET. – Darin Dimitrov May 19 '11 at 16:46
  • @Tom, this is a full blown WebForm. You can call it Foo.aspx and include in your project and navigate to `~/foo.aspx` to see the result. Markup and code behind have been put in the same file to simplify but you probably could have a separate code behind file. We are getting a little of topic here. And the topic is: use the `FindControl` method on the sender in the `SelectedIndexChanged` event to locate the corresponding label. – Darin Dimitrov May 19 '11 at 16:49