How to get the selected dropdown value on a nested gridview row command

how can I get the selected dropdownlist value for the gridview subcommands command.

I have a nested gridview on my page, inside the second gridview I want to use the drop-down list for the driver. I am trying to get the selected value for this dropdown, but could not get it in gridcom rowcommand. I am also trying to click the mouse button, but I cannot get it.

<asp:GridView ID="dtgNewTrips" runat="server" AllowPaging="true" AutoGenerateColumns="false" CssClass="table table-striped table-bordered table-hover" OnRowDataBound="dtgNewTrips_RowDataBound" DataKeyNames="Trips_ID"> <Columns> <asp:TemplateField ItemStyle-Width="20px"> <ItemTemplate> <a href="JavaScript:ViewDetails('div<%# Eval(" Trips_ID ") %>');"> <img alt="City" id="imgdiv<%# Eval("Trips_ID") %>" src="Images/Icons/plusicon.png" /> </a> <div id="div<%# Eval(" Trips_ID ") %>" style="display: none;"> <asp:GridView ID="dtgViewDetails" runat="server" AutoGenerateColumns="false" DataKeyNames="Trips_ID" CssClass="ChildGrid" ShowHeader="false" OnRowDataBound="dtgViewDetails_RowDataBound" OnRowCommand="dtgViewDetails_RowCommand"> <Columns> <asp:TemplateField ShowHeader="false"> <ItemTemplate> <div> <table style="width: 100%;"> <tr> <td>Adults: <asp:Label ID="lblTrip_ID" runat="server" Text='<%#Eval("Trips_ID") %>' Visible="false"></asp:Label> <asp:Label ID="Label1" runat="server" Text='<%#Eval("AdultsCount") %>'></asp:Label> </td> <td>Children: <asp:Label ID="Label2" runat="server" Text='<%#Eval("ChildrensCount") %>'></asp:Label> </td> <td>Passenger Comments: <asp:Label ID="Label4" runat="server" Text='<%#Eval("PassengerComments") %>'></asp:Label> </td> </tr> <tr> <td>Trip Stops:</td> <td> <asp:Label ID="Label3" runat="server" Text='<%#Eval("TripStops") %>'></asp:Label> </td> <td>TripStops Comments: <asp:Label ID="Label5" runat="server" Text='<%#Eval("TripStopsComments") %>'></asp:Label> </td> </tr> <tr> <td> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:DropDownList ID="ddlDriver" runat="server" DataTextField="DriverName" DataValueField="DriversInfo_ID" CssClass="form-control" AutoPostBack="false"></asp:DropDownList> </ContentTemplate> </asp:UpdatePanel> </td> <td> <asp:Button ID="btnAssign" runat="server" Text="Assign To Driver" CssClass="btn btn-primary" CommandArgument='<%# ((GridViewRow)Container).RowIndex %>' CommandName="Assign" /> </td> <td> <asp:Button ID="btnAssignToAll" runat="server" OnClick="btnAssignToAll_Click" Text="Notify All Drivers" CssClass="btn btn-primary" /> <asp:Button ID="btnCancelTrip" runat="server" OnClick="btnCancelTrip_Click" Text="Cancel Trip" CssClass="btn btn-warning" />&nbsp; </td> </tr> </table> </div> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Trip No"> <ItemTemplate> <asp:Label ID="lblTripRefID" runat="server" Text='<%#Eval("TripRefID") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Customer Name"> <ItemTemplate> <asp:Label ID="lblCompanyName" runat="server" Text='<%#Eval("CompanyName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Pickup Location"> <ItemTemplate> <asp:Label ID="lblPickupLocation" runat="server" Text='<%#Eval("PickupLocation") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> 

C # code

 protected void dtgViewDetails_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Assign") { int rowIndex = Convert.ToInt32(e.CommandArgument); GridView grid = (GridView)sender; if (grid.ID == "dtgViewDetails") { Label lblTrip_ID = (Label)grid.Rows[rowIndex].FindControl("lblTrip_ID"); DropDownList ddlDriver = (DropDownList)grid.Rows[rowIndex].FindControl("ddlDriver"); } } } 

But I get the default dropdownlist null index value. please help .. thanks in advance.

+5
source share

All Articles