源代码: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);
}
分享到:
相关推荐
ASP.NET2.0中编程实现GridView与FormView的主-详细视图
这是用vs建的一个例子,关于GridView控件和FormView控件的。
详细的介绍了fromview的使用方法,加上本人实例
FormView控件的运用
formview.css
GridView_+_FormView_+_SqlDataSource
网站上自己下载的代码 运行了一遍 有一些收获 有兴趣的可以自己下载看看 免费下载的哦
formview分页上一页下一页总页数当前页数首页末页
一个基于FormView的扩展类,允许控件以不同形式随窗体变化
这个是Windows FormView的程序,实现基本的FormView功能。
控制了窗口的大小当拖动窗口缩小的添加的空间大小时,窗口不会再缩小了,实现了formview dialog对话框的布局,还有就是点击菜单项切换界面,
FormView 视图 无法响应Ctrl+C等键盘消息解决办法 仅4行代码 无需对单个Editbox控件修改,非常好用 仅需2资源分
当使用GridView、DetailsView或FormView控件的内建插入、编辑或删除特征时,在用户添加一条新记录或更新/删除一条现在记录的过程中发生了多个步骤。正如我们之前一节里所讨论的,在GridView中编辑一行时,保存...
论坛上有很多人使用对话框的scrollBar控制对话框中一部分的卷动,实现过程复杂而且逻辑混乱。这个例子里,直接在对话框中创建一个CFormView子窗口,子窗口自己管理卷动,各负其责,条理清晰
listbox,datasousce 应用以及
基于FormView的多语言界面程序,通过定义多种语言的lng文件,即可轻松实现语言的切换
GridView与FormView结合使用,实现增删改查,第一种方法,用数据源绑定完成!第二种方法,全部用代码完成!
开发环境 VS2010+SQLServer2005 W7系统 使用技术:ASP.NET DIV+CSS 使用控件:母板页、主题、站点导航、树形菜单、GridView、FormView、Repeater、DataList 高级功能:真分页、批量删除、光棒、购物车(在线购物和...
在ASP.NET 2.0中操作数据:使用FormView 的模板(源码)