简体   繁体   中英

Get the selected value of an asp dropdownlist in an asp repeater

I have a repeater that has multiple dropdown lists and multiple buttons. Each button is tied to the dropdown list in the repeater row. Is there a way to alert the dropdownlists value on a button click. I want the button click to alert the value of the ddl in the same repeater row.

Is this possible?

There must be something that relates the button to the corresponding dropdown list. You mentioned something about a row . But in an ASP.NET repeater there's nothing that forces you to separate rows.

But let's suppose that you have separated your rows with a <div> :

<asp:Repeater ID="rep" runat="server">
    <ItemTemplate>
        <div>
            <asp:LinkButton runat="server" Text="Click me" CssClass="btn" />
            <asp:DropDownList runat="server" DataSource="foo" DataValueField="Value" DataTextField="Text" />
        </div>
    </ItemTemplate>
</asp:Repeater>

Now you could subscribe to the click event of the button and find the corresponding dropdown:

$(function() {
    $('.btn').click(function() {
        var ddl = $(this).closest('div').find('select');
        var selectedValue = ddl.val();
        alert(selectedValue);
        return false;
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM