从gridview c#asp.net里面获取下拉列表列表项

时间:2022-02-27 11:18:36

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

需要帮助通过复选框获取每个gridview行的下拉列表选定值。我看了所有这些例子: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:

但他们都没有帮助过我。根据我的理解,下拉列表必须从gridview> gridview row>然后下拉列表中进行定位。我试过这个:

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.

但我得到的InvalidCastException未被NamingContainer行的用户代码处理。从数字转换时,该值必须是小于无穷大的数字。谢谢。

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();

}

1 个解决方案

#1


0  

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

就像Seano666所说的那样,你将一个Button转换为DropDownList,因此InvalidCastException。

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

你可以做的只是简单地使用你在foreach循环中已经拥有的GridViewRow。

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

#1


0  

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

就像Seano666所说的那样,你将一个Button转换为DropDownList,因此InvalidCastException。

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

你可以做的只是简单地使用你在foreach循环中已经拥有的GridViewRow。

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