Handle OnSelectedIndexChanged from a DropDownList inside ItemTemplate of a Listview

Posted on 2011-10-14
Last Modified: 2012-05-12
Hello. I have a DropDownList control with fixed listitems in every line of a listview. The ItemTemplate looks like this:
                <tr class="<%# (Container.DataItemIndex % 2 == 0)?"row":"altrow" %>">
                    <td><%# Eval("TruckId") %></td>
                        <asp:DropDownList id="ddlOperational" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlOperationalChanged">
                            <asp:ListItem Value="0">A</asp:ListItem>
                            <asp:ListItem Value="1">M</asp:ListItem>
                            <asp:ListItem Value="2">T</asp:ListItem>
                    <td><%# Eval("OperatorName") %></td>

Open in new window

Now, inside ddlOperationalChanged, I must set operational status of a truck. But how do I know which truck (line of listview, TruckId) that had its operational status changed?

    protected void ddlOperationalChanged(Object sender, EventArgs e)
        if (sender is DropDownList)
            DropDownList ddl = (DropDownList)sender;
            lblTest.Text = ddl.SelectedValue;

Open in new window

Question by:lefodnes
    LVL 17

    Accepted Solution

    Try this

    What you need to do assign datakeys (like DataKeyNames="TruckId") to listview

    protected void ddlOperationalChanged(Object sender, EventArgs e)
            if (sender is DropDownList)
              DropDownList ddl = (DropDownList)sender;
              ListViewDataItem row = (ListViewDataItem)ddl.NamingContainer;

              string TruckID = (string)lvOrderItems.DataKeys[row.DisplayIndex]["TruckId"];

    Author Closing Comment

    Just what I wanted. Thank you!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Suggested Solutions

    This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
    In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now