[英]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.