需要帮助获取通过复选框选中的每个gridview行的下拉列表选定值。我看了所有这些例子: http://www.ezineasp.net/post/Gridview-DropDownList-SelectedValue-in-ASP-Net.aspx
但他们都没有帮助过我。根据我的理解,下拉列表必须从gridview>中定位。 gridview row>然后是下拉列表。我试过这个:
GridViewRow grdrow = (GridViewRow)((DropDownList)sender).NamingContainer;
DropDownList ddl = (DropDownList)grdWiperList.Rows[grdrow.RowIndex].FindControl("ddlQuantity");
但是我在NamingContainer行获得InvalidCastException was unhandled by user code
。从数字转换时,该值必须是小于无穷大的数字。谢谢。
我的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>
我的代码背后:
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();
}
答案 0 :(得分:0)
就像Seano666所说,你将一个Button转换为DropDownList,因此InvalidCastException。
你可以做的只是简单地使用你在foreach循环中已经拥有的GridViewRow。
DropDownList ddlQuantity = (DropDownList)row.FindControl("ddlQuantity");
string qty = ddlQuantity.SelectedValue;