简体   繁体   English

从GridView C#ASP.NET内部获取下拉列表列表项

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

Need help getting the dropdownlist selected value for each gridview row checked via checkbox. 需要帮助,以通过复选框选中每个gridview行的下拉列表选定值。 I have looked at all these examples: http://www.ezineasp.net/post/Gridview-DropDownList-SelectedValue-in-ASP-Net.aspx 我看了所有这些示例: 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 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. 据我了解,下拉列表必须从gridview> gridview row>然后选择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. 但是我得到了NamingContainer行中的InvalidCastException was unhandled by user code When casting from a number, the value must be a number less than infinity. 从数字强制转换时,该值必须是小于无穷大的数字。 Thank you. 谢谢。

My aspx: 我的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. 就像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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从数据库asp.net C#中查询为gridview中的dropdownlist设置ToolTip - set ToolTip for dropdownlist inside gridview from query from database asp.net C# 在c#asp.net中启用DropdownList的列表项 - Enabling List item of DropdownList in c# asp.net ASP.NET和C#-在静态DropDownList中获取所选项目 - ASP.NET and C# - Get selected item in static DropDownList 从下拉列表 ASP.NET MVC 5 和 C# 中删除项目 - Remove item from dropdownlist ASP.NET MVC 5 and C# GridView中的DropDownList绑定到ASP.NET C#中GridView内TextBox中DropDown的选定值 - DropDownList in a GridView to bind the selected value of DropDown in TextBox inside a GridView in asp.net c# 从List集合c#/ asp.net填充Dropdownlist - Populating Dropdownlist from List collection c#/asp.net Gridview带有来自数据库的下拉列表,并在gridview外部添加,编辑删除按钮? ASP.NET C# - Gridview with dropdownlist from the database and outside add,edit delete buttons outside the gridview ? ASP.NET C# 在C#中的asp.net gridview模板字段中找到dropdownlist - Locating dropdownlist inside of asp.net gridview template field in C# 如何使用C#将数据源分配给gridview ASP.net中的转发器内的下拉列表 - how to assign a datasource to a dropdownlist inside a repeater in a gridview ASP.net with C# C#Asp.Net使用DropDownList更改GridView的数据源 - C# Asp.Net changing DataSource for GridView using DropDownList
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM