博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
网上购物系统(Task010)——FormView编辑更新商品详细信息
阅读量:6095 次
发布时间:2019-06-20

本文共 9381 字,大约阅读时间需要 31 分钟。

源代码:13033480群共享

一、前期准备

编辑商品信息属于管理功能,管理功能的网页最好单独放在一个文件夹中,为此,做一些准备工作:

1、修改母版页中的路径为绝对路径

2、复制Controls中的ItemDetailsControl.ascx,改名为ItemManageControl.ascx

3、在ItemManageControl.ascx中的FormViewItemPlate模板中添加三个LinkButton按钮,编辑、新建、删除。设置一下单元格右对齐,会美观一点。

4、临时在ItemDetails.aspx中添加代码:

Response.Redirect("Manager/ItemManage.aspx" + Request.Url.Query);

5Web中新建文件夹Manager,并添加ItemManage.aspx,引用母版页

二、编辑EditItemTemplate模板,可直接将模板ItemTemplate复制过来进行修改,添加必要的文本框、下拉列表框,模板代码如下:

(注意:<%@OutputCacheDuration="100000"VaryByParam="page;categoryId"%>页面缓存要去掉,否则,嘿嘿嘿)

商品类别:
商品名称:
商品价格:
商品描述:
供应时间:
供应日期:
供应地区:

 

三、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 = new List
(); 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("
"); } } 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);        }

版权所有©2012,西园电脑工作室.欢迎转载,转载请注明出处.更多文章请参阅博客

转载于:https://www.cnblogs.com/WestGarden/archive/2012/05/11/3138427.html

你可能感兴趣的文章
架构师之路(一)- 什么是软件架构
查看>>
第十二周项目4-点、圆的关系
查看>>
团队项目计划会议
查看>>
使用C3P0连接池
查看>>
iOS汉字中提取首字母
查看>>
设计模式之工厂模式
查看>>
jquery的冒泡和默认行为
查看>>
Check failed: error == cudaSuccess (7 vs. 0) too many resources requested for launch
查看>>
USACO 土地购买
查看>>
【原创】远景能源面试--一面
查看>>
B1010.一元多项式求导(25)
查看>>
10、程序员和编译器之间的关系
查看>>
前端学习之正则表达式
查看>>
配置 RAILS FOR JRUBY1.7.4
查看>>
Beta阶段——Scrum 冲刺博客第五天
查看>>
指令汇B新闻客户端开发(五) ShareSdk的使用
查看>>
AndroidStudio中导入SlidingMenu报错解决方案
查看>>
http://www.blogjava.net/pdw2009/archive/2007/10/08/151180.html
查看>>
cocos2d-x 画
查看>>
hadoop(6)---mapred-site.xml 详解以及常用配置。
查看>>