我正在开展一个学校项目,以创建一个门户网站,允许屠夫注册并在线销售他们的肉。现在我创建了两个 girdview,一个用于我的默认目录(显示产品以允许屠夫在创建目录时选择他们的产品),另一个用于从 gridview1 到 gridview2 获取项目。现在在 gridview2 中,我希望屠夫能够选择和编辑产品,然后将它们保存到他们自己的 ButcherProducts 表中,该表具有 StoreID、ProductID、价格、数量。现在我可以从 gridview1 到 gridview2 获取项目,但我正在努力放置编辑功能,以便屠夫可以编辑和保存。这是我的示例代码。我首先使用实体框架代码。
选择类别:
<td> <asp:GridView ID="Catalog" runat="server"
AutoGenerateColumns="False"
ItemType="DataAccess.Product"
AllowSorting="false"
DataKeyNames="ProductID"
SelectMethod="GetCatalog">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID"
SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="Product Name"
SortExpression="ProductName" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID"
SortExpression="CategoryID" />
<asp:TemplateField HeaderText="Category">
<ItemTemplate>
<%#:Item.Category.CategoryName %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<img src="..Checkout/images/<%#:Item.ImagePath%>"
width="20" height="25" border="1" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Select Products">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
后面的 C# 代码:
List<product> tempIDList = new List<product>();
protected void Page_Load(object sender, EventArgs e)
{
}
private MeatbyMobileContext _db = new MeatbyMobileContext();
public IQueryable<category> GetCatalogCategories()
{
return _db.Categories;
}
public IQueryable<product> GetCatalog([Control]int? category)
{
var query = _db.Products.Select(p => p);
if (category.HasValue)
{
query = query.Where(p => p.CategoryID == category.Value);
}
return query;
}
protected void Button4_Click(object sender, EventArgs e)
{
for (int z = 0; z < Catalog.Rows.Count; z++)
{
MeatbyMobileContext db = new MeatbyMobileContext();
CheckBox chk = (CheckBox)Catalog.Rows[z].FindControl("CheckBox1");
if (chk.Checked)
{
int dKey = Convert.ToInt32( Catalog.DataKeys[z].Value);
Product product = (from c in db.Products
where c.ProductID.Equals(dKey)
select c).SingleOrDefault();
tempIDList.Add(product );
}
}
GridView2.DataSource = tempIDList;
GridView2.DataBind();