源代码:13033480群共享
一、前期准备
编辑商品信息属于管理功能,管理功能的网页最好单独放在一个文件夹中,为此,做一些准备工作:
1、修改母版页中的路径为绝对路径
2、复制Controls中的ItemDetailsControl.ascx,改名为ItemManageControl.ascx
3、在ItemManageControl.ascx中的FormView的ItemPlate模板中添加三个LinkButton按钮,编辑、新建、删除。设置一下单元格右对齐,会美观一点。
4、临时在ItemDetails.aspx中添加代码:
Response.Redirect("Manager/ItemManage.aspx" + Request.Url.Query);
5、Web中新建文件夹Manager,并添加ItemManage.aspx,引用母版页
二、编辑EditItemTemplate模板,可直接将模板ItemTemplate复制过来进行修改,添加必要的文本框、下拉列表框,模板代码如下:
(注意:<%@OutputCacheDuration="100000"VaryByParam="page;categoryId"%>页面缓存要去掉,否则,嘿嘿嘿)
<EditItemTemplate> <table cellpadding="0" cellspacing="0"> <tr> <td height="18" width="18"> </td> <td height="18" width="96"> </td> <td height="18" width="18"> </td> <td height="18" width="96"> </td> <td height="18" width="180"> </td> </tr> <tr> <td style="height: 18px" width="18"> </td> <td rowspan="8" width="96"> <asp:Image ID="imgItem" runat="server" AlternateText='<%# Eval("Name") %>' Height="144" ImageUrl='<%# Eval("Image") %>' Width="144" /></td> <td style="height: 18px" width="18"> </td> <td colspan="2" style="height: 18px"> <asp:FileUpload ID="fupImage" runat="server" Width="80%" /> <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="上传" /></td> </tr> <tr> <td width="18"> </td> <td width="18"> </td> <td width="96"> 商品类别:</td> <td width="180"> <asp:DropDownList ID="ddlCategories" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlCategories_SelectedIndexChanged"> </asp:DropDownList> </td> </tr> <tr> <td width="18"> </td> <td width="18"> </td> <td width="96"> 商品名称:</td> <td width="180"> <asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox> </td> </tr> <tr> <td width="18"> </td> <td width="18"> </td> <td width="96"> 商品价格:</td> <td width="180"> <asp:TextBox ID="txtPrice" runat="server" Text='<%# Bind("Price") %>'></asp:TextBox> </td> </tr> <tr> <td width="18"> </td> <td width="18"> </td> <td width="96"> 商品描述:</td> <td width="180"> <asp:TextBox ID="txtDescn" runat="server" Text='<%# Bind("Descn") %>'></asp:TextBox> </td> </tr> <tr> <td width="18"> </td> <td width="18"> </td> <td width="96"> 供应时间:</td> <td width="180"> <asp:TextBox ID="txtSupplyTime" runat="server" Text='<%# Bind("SupplyTime") %>'></asp:TextBox> </td> </tr> <tr> <td width="18"> </td> <td width="18"> </td> <td width="96"> 供应日期:</td> <td width="180"> <asp:TextBox ID="txtSupplyDate" runat="server" Text='<%# Bind("SupplyDate") %>'></asp:TextBox> </td> </tr> <tr> <td width="18"> </td> <td width="18"> </td> <td width="96"> 供应地区:</td> <td width="180"> <asp:TextBox ID="txtSupplyArea" runat="server" Text='<%# Bind("SupplyArea") %>'></asp:TextBox> </td> </tr> <tr> <td height="18" width="18"> </td> <td height="18" width="96"> </td> <td height="18" width="18"> </td> <td height="18" width="96"> </td> <td height="18" width="180" align="right"> <asp:LinkButton ID="lbtnDelete" runat="server" CommandName="Update" Text="更新" /> <asp:LinkButton ID="lbtnNew" runat="server" CommandName="Cancel" Text="取消" /> </td> </tr> </table> </EditItemTemplate>
三、ItemManageControl.ascx的后台代码:
代码页的主任是读取前台窗体的值,主要是FileUpload控件和DropDownList控件的值,两个值,分别使用了ViewState存储了信息。读取数据的时候,注意,如果用户没有做修改,刚使用原来的值,这个值放在了privatestaticIList<ItemDetails> itemdetails =newList<ItemDetails>();中。
using System; using System.Web; using System.Web.UI.WebControls; using System.Collections.Generic; using WestGarden.DAL; using WestGarden.Model; namespace WestGarden.Web { public partial class ItemManageControl : System.Web.UI.UserControl { private static IList<ItemDetails> itemdetails = new List<ItemDetails>(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindFormView(); ViewState["ImageUrl"] = null; ViewState["SelectedCategoryId"] = null; } } protected void fvwItemDetails_ModeChanging(object sender, FormViewModeEventArgs e) { switch (e.NewMode) { case FormViewMode.Edit: this.fvwItemDetails.ChangeMode(FormViewMode.Edit); BindFormView(); DropDownList ddl = (DropDownList)fvwItemDetails.FindControl("ddlCategories"); BindDropDownList(ddl); break; case FormViewMode.ReadOnly: this.fvwItemDetails.ChangeMode(FormViewMode.ReadOnly); BindFormView(); break; default: break; } } protected void fvwItemDetails_PreRender(object sender, EventArgs e) { } protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e) { DropDownList ddl = (DropDownList)fvwItemDetails.FindControl("ddlCategories"); ViewState["SelectedCategoryId"] = ddl.SelectedValue; } protected void btnUpload_Click(object sender, EventArgs e) { FileUpload fup = (FileUpload)fvwItemDetails.FindControl("fupImage"); if (fup.HasFile) { fup.SaveAs(Server.MapPath("~/Images/Items/") + fup.FileName); Image img = (Image)fvwItemDetails.FindControl("imgItem"); img.ImageUrl = "~/Images/Items/" + fup.FileName.ToString(); ViewState["ImageUrl"] = "~/Images/Items/" + fup.FileName.ToString(); } else { Response.Write("<script>alert('请先浏览并选择图片')</script>"); } } protected void fvwItemDetails_ItemUpdating(object sender, FormViewUpdateEventArgs e) { if (ViewState["ImageUrl"] != null) { itemdetails[0].Image = ViewState["ImageUrl"].ToString(); } if (ViewState["SelectedCategoryId"] != null) { DropDownList ddl = (DropDownList)fvwItemDetails.FindControl("ddlCategories"); itemdetails[0].CategoryId = ViewState["SelectedCategoryId"].ToString(); } TextBox txtname = (TextBox)fvwItemDetails.FindControl("txtName"); itemdetails[0].Name = txtname.Text; TextBox txtPrice = (TextBox)fvwItemDetails.FindControl("txtPrice"); itemdetails[0].Price = decimal.Parse(txtPrice.Text); TextBox txtDescn = (TextBox)fvwItemDetails.FindControl("txtDescn"); itemdetails[0].Descn = txtDescn.Text; TextBox txtSupplyTime = (TextBox)fvwItemDetails.FindControl("txtSupplyTime"); itemdetails[0].SupplyTime = txtSupplyTime.Text; TextBox txtSupplyDate = (TextBox)fvwItemDetails.FindControl("txtSupplyDate"); itemdetails[0].SupplyDate = txtSupplyDate.Text; TextBox txtSupplyArea = (TextBox)fvwItemDetails.FindControl("txtSupplyArea"); itemdetails[0].SupplyArea = txtSupplyArea.Text; Item item = new Item(); item.UpdateItem(itemdetails[0]); fvwItemDetails.ChangeMode(FormViewMode.ReadOnly); BindFormView(); ViewState["ImageUrl"] = null; ViewState["SelectedCategoryId"] = null; } private void BindFormView() { int itemKey = int.Parse(Request.QueryString["itemId"]); Item item = new Item(); itemdetails = item.GetItemDetailsByItemId(itemKey); fvwItemDetails.DataSource = itemdetails; fvwItemDetails.DataBind(); } private void BindDropDownList(DropDownList ddl) { ddl.DataSource = new Category().GetCategories(); ddl.DataTextField = "Name"; ddl.DataValueField = "CategoryId"; ddl.DataBind(); string selectcategory = Request.QueryString["categoryId"].ToString(); if (selectcategory != null) { ListItem selectedItem = ddl.Items.FindByValue(selectcategory); if (selectedItem != null) selectedItem.Selected = true; } } } }
四、数据访问层DAL中的Item.cs类中添加更新函数UpdateItem(),代码如下:
public void UpdateItem(ItemDetails item) { SqlParameter[] parms; parms = new SqlParameter[] { new SqlParameter("@ItemId",SqlDbType.Int), new SqlParameter("@CategoryId",SqlDbType.VarChar,20), new SqlParameter("@Name",SqlDbType.VarChar,80), new SqlParameter("@Price",SqlDbType.Decimal,10), new SqlParameter("@Image",SqlDbType.VarChar,80), new SqlParameter("@Descn",SqlDbType.VarChar,80), new SqlParameter("@SupplyTime",SqlDbType.VarChar,80), new SqlParameter("@SupplyDate",SqlDbType.VarChar,80), new SqlParameter("@SupplyArea",SqlDbType.VarChar,80) }; parms[0].Value = item.ItemId; parms[1].Value = item.CategoryId; parms[2].Value = item.Name; parms[3].Value = item.Price; parms[4].Value = item.Image; parms[5].Value = item.Descn; parms[6].Value = item.SupplyTime; parms[7].Value = item.SupplyDate; parms[8].Value = item.SupplyArea; SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_UPDATE_ITEM, parms); }