【发布时间】:2021-05-19 05:42:32
【问题描述】:
我想在我的代码中获得一些帮助,我想要做的是填充我的下拉列表,该列表显示可用数量(存储在数据库中)但是当我尝试在行中找到控件时得到一个空引用异常,它说行为空。
这是包含我的下拉列表的模板字段
<asp:TemplateField HeaderText="Quantity" SortExpression="Quantity">
<EditItemTemplate>
<asp:DropDownList ID="ddlQuantity" runat="server">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Quantity") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
还有我的代码
protected void Cart_GV_RowEditing(object sender, GridViewEditEventArgs e) {
Cart_GV.EditIndex = e.NewEditIndex;
GridViewRow row = (sender as Control).Parent.Parent as GridViewRow;
DropDownList ddlQuantity = row.FindControl("ddlQuantity") as DropDownList;
string cartitemid = Cart_GV.DataKeys[e.NewEditIndex].Value.ToString();
conn.Open();
SqlCommand checkQuantity = new SqlCommand("SELECT OnHandQTY FROM Inventory INNER JOIN Cart_Item ON Inventory.ProductID = Cart_Item.ProductID WHERE CartItemID = '" + cartitemid + "';", conn);
int availalbleQuantity = (int)checkQuantity.ExecuteScalar();
conn.Close();
for (int i = 1; i <= availalbleQuantity; i++) {
ListItem item = new ListItem(i.ToString(), i.ToString());
ddlQuantity.Items.Add(item);
}
ViewUserCart();
}
【问题讨论】: