Handle OnSelectedIndexChanged from a DropDownList inside ItemTemplate of a Listview

Hello. I have a DropDownList control with fixed listitems in every line of a listview. The ItemTemplate looks like this:
            <ItemTemplate>
                <tr class="<%# (Container.DataItemIndex % 2 == 0)?"row":"altrow" %>">
                    <td><%# Eval("TruckId") %></td>
                    <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>
                        </asp:DropDownList>
                    </td>
                    <td><%# Eval("OperatorName") %></td>
                </tr>
            </ItemTemplate> 

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

lefodnesAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
sonawanekiranConnect With a Mentor Commented:
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"];
        }
}
0
 
lefodnesAuthor Commented:
Just what I wanted. Thank you!
0
All Courses

From novice to tech pro — start learning today.