简体   繁体   中英

Get dropdownlist list item from inside gridview c# asp.net

Need help getting the dropdownlist selected value for each gridview row checked via checkbox. I have looked at all these examples: http://www.ezineasp.net/post/Gridview-DropDownList-SelectedValue-in-ASP-Net.aspx

http://www.aspsnippets.com/Articles/Populate-DropDownList-with-Selected-Value-in-EditItemTemplate-of-GridView-in-ASPNet.aspx

But none of them has helped me. From what I understand, the dropdownlist has to be targeted from gridview > gridview row > then dropdownlist. I have tried this:

GridViewRow grdrow = (GridViewRow)((DropDownList)sender).NamingContainer;
DropDownList ddl = (DropDownList)grdWiperList.Rows[grdrow.RowIndex].FindControl("ddlQuantity");

But I'm getting InvalidCastException was unhandled by user code at the NamingContainer line. When casting from a number, the value must be a number less than infinity. Thank you.

My aspx:

<asp:GridView ID="grdWiperList" runat="server"  AutoGenerateColumns = "false" DataKeyNames="wiperID" 
                Font-Names = "Arial" Font-Size = "11pt" HeaderStyle-BackColor = "#d5d1c9" >
            <Columns>
                <asp:TemplateField HeaderText="Select">
                     <ItemTemplate>
                        <asp:CheckBox ID="IndividualChkBox" runat="server" onclick = "Check_Click(this)"/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField ItemStyle-Width = "150px" DataField = "Description" HeaderText = "Description"/>
                <asp:BoundField ItemStyle-Width = "150px" DataField = "Emplacement" HeaderText = "Emplacement"/>
                <asp:BoundField ItemStyle-Width = "75px" DataField = "ItemNo" HeaderText = "ItemNo"/>
                <asp:BoundField ItemStyle-Width = "100px" DataField = "Price" HeaderText = "Prix"/>
                <asp:TemplateField headertext="Qty">
                    <ItemTemplate>
                        <asp:DropDownList ID="ddlQuantity" runat="server">
                        <asp:ListItem Value="0">--Select--</asp:ListItem>
                        <asp:ListItem Value="1">1</asp:ListItem>
                        <asp:ListItem Value="2">2</asp:ListItem>
                        <asp:ListItem Value="3">3</asp:ListItem>
                        <asp:ListItem Value="4">4</asp:ListItem>
                        <asp:ListItem Value="5">5</asp:ListItem>
                        <asp:ListItem Value="6">6</asp:ListItem>
                        <asp:ListItem Value="7">7</asp:ListItem>
                        <asp:ListItem Value="8">8</asp:ListItem>
                        <asp:ListItem Value="8">9</asp:ListItem>
                        </asp:DropDownList>
                    </ItemTemplate>
                     </asp:TemplateField>
             </Columns>
        </asp:GridView>
        <br />
        <br />
        <asp:Button ID="btnAddToCart" runat="server" Text="Add Selected to Cart" Visible="False" OnClick="AddToCart"/>
        </div>

My code behind:

protected void AddToCart(object sender, EventArgs e)
{

    foreach (GridViewRow row in grdWiperList.Rows)
    {
        CheckBox chkb = (CheckBox)row.FindControl("IndividualChkBox");

        if (chkb.Checked)
        {

            var selectedProducts = grdWiperList.DataKeys[row.RowIndex].Value.ToString().ToList();

            GridViewRow grdrow = (GridViewRow)((DropDownList)sender).NamingContainer;
            DropDownList ddl = (DropDownList)grdWiperList.Rows[grdrow.RowIndex].FindControl("ddlQuantity");
            string qty = ddl.SelectedValue.ToString();
            Session["qty"] = ddl.SelectedValue.ToString();
            Session["prod"] = grdWiperList.DataKeys[row.RowIndex].Value.ToString().ToList();
        }
        else
        {
            //do something here
        }

    }

    //before checkout remove the checks so that it does not display in the shopping cart
    foreach (GridViewRow row in grdWiperList.Rows)
    {
        CheckBox cb = (CheckBox)row.FindControl("IndividualChkBox");
        if (cb.Checked)
            cb.Checked = false;
    }

    Checkout();

}

Like Seano666 said, you are casting a Button into a DropDownList, hence the InvalidCastException.

What you could do instead is just simply use the GridViewRow you already have from your foreach loop.

DropDownList ddlQuantity = (DropDownList)row.FindControl("ddlQuantity");
string qty = ddlQuantity.SelectedValue;

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