0

我正在开展一个学校项目,以创建一个门户网站,允许屠夫注册并在线销售他们的肉。现在我创建了两个 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();
4

1 回答 1

0

使用编辑项模板。它是专门为编辑而创建的。在您的网格视图中,为需要像这样编辑的内容创建相应的编辑项模板。

<asp:TemplateField HeaderText="Category">

       <ItemTemplate>
     <%#:Item.Category.CategoryName %>
        </ItemTemplate>
       <EditItemTemplate>
       <asp:TextBox  ID="txtCategoryEdit"  runat="server"  Text='<%#Bind("CategoryName")%>' /  >
           <EditItemTemplate>

    </asp:TemplateField>

并在您的行更新事件中找到这个文本框,如下所示

 protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
       TextBox txtCategoryEdit = (TextBox)GridView2.Rows(e.RowIndex).FindControl("FieldEditTextBox")  ;

            // put your update logic here 
    }

还将您的 Gridview2 绑定到对象数据源并指定一个虚拟更新方法。这将为您节省大量工作。

于 2013-10-08T07:16:57.223 回答