电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

FormView编辑更新商品详细信息


发布日期:2020/5/25
 
前期准备

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

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

复制Controls中的ItemDetailsControlascx改名为ItemManageControlascx

在ItemManageControlascx中的FormView的ItemPlate模板中添加三个LinkButton按钮编辑新建删除设置一下单元格右对齐会美观一点

临时在ItemDetailsaspx中添加代码

ResponseRedirect(Manager/ItemManageaspx + RequestUrlQuery)

Web中新建文件夹Manager并添加ItemManageaspx引用母版页

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

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

[html] <EditItemTemplate>

<table cellpadding= cellspacing=>

<tr>

<td height= width=>

</td>

<td height= width=>

</td>

<td height= width=>

</td>

<td height= width=>

</td>

<td height= width=>

</td>

</tr>

<tr>

<td width=>

</td>

<td rowspan= width=>

<asp:Image ID=imgItem runat=server AlternateText=<%# Eval(Name) %> Height=

ImageUrl=<%# Eval(Image) %> Width= /></td>

<td width=>

</td>

<td colspan= >

<asp:FileUpload ID=fupImage runat=server Width=% />

<asp:Button ID=btnUpload runat=server OnClick=btnUpload_Click Text=上传 /></td>

</tr>

<tr>

<td width=>

</td>

<td width=>

</td>

<td width=>

商品类别</td>

<td width=>

<asp:DropDownList ID=ddlCategories runat=server

AutoPostBack=True

OnSelectedIndexChanged=ddlCategories_SelectedIndexChanged>

</asp:DropDownList>

</td>

</tr>

<tr>

<td width=>

</td>

<td width=>

</td>

<td width=>

商品名称</td>

<td width=>

<asp:TextBox ID=txtName runat=server Text=<%# Bind(Name) %>></asp:TextBox>

</td>

</tr>

<tr>

<td width=>

</td>

<td width=>

</td>

<td width=>

商品价格</td>

<td width=>

<asp:TextBox ID=txtPrice runat=server Text=<%# Bind(Price) %>></asp:TextBox>

</td>

</tr>

<tr>

<td width=>

</td>

<td width=>

</td>

<td width=>

商品描述</td>

<td width=>

<asp:TextBox ID=txtDescn runat=server Text=<%# Bind(Descn) %>></asp:TextBox>

</td>

</tr>

<tr>

<td width=>

</td>

<td width=>

</td>

<td width=>

供应时间</td>

<td width=>

<asp:TextBox ID=txtSupplyTime runat=server Text=<%# Bind(SupplyTime) %>></asp:TextBox>

</td>

</tr>

<tr>

<td width=>

</td>

<td width=>

</td>

<td width=>

供应日期</td>

<td width=>

<asp:TextBox ID=txtSupplyDate runat=server Text=<%# Bind(SupplyDate) %>></asp:TextBox>

</td>

</tr>

<tr>

<td width=>

</td>

<td width=>

</td>

<td width=>

供应地区</td>

<td width=>

<asp:TextBox ID=txtSupplyArea runat=server Text=<%# Bind(SupplyArea) %>></asp:TextBox>

</td>

</tr>

<tr>

<td height= width=>

</td>

<td height= width=>

</td>

<td height= width=>

</td>

<td height= width=>

</td>

<td height= width= align=right>

<asp:LinkButton ID=lbtnDelete runat=server CommandName=Update Text=更新 />

<asp:LinkButton ID=lbtnNew runat=server CommandName=Cancel Text=取消 />

</td>

</tr>

</table>

</EditItemTemplate>

<EditItemTemplate>

<table cellpadding= cellspacing=>

<tr>

<td height= width=>

</td>

<td height= width=>

</td>

<td height= width=>

</td>

<td height= width=>

</td>

<td height= width=>

</td>

</tr>

<tr>

<td width=>

</td>

<td rowspan= width=>

<asp:Image ID=imgItem runat=server AlternateText=<%# Eval(Name) %> Height=

ImageUrl=<%# Eval(Image) %> Width= /></td>

<td width=>

</td>

<td colspan= >

<asp:FileUpload ID=fupImage runat=server Width=% />

<asp:Button ID=btnUpload runat=server OnClick=btnUpload_Click Text=上传 /></td>

</tr>

<tr>

<td width=>

</td>

<td width=>

</td>

<td width=>

商品类别</td>

<td width=>

<asp:DropDownList ID=ddlCategories runat=server

AutoPostBack=True

OnSelectedIndexChanged=ddlCategories_SelectedIndexChanged>

</asp:DropDownList>

</td>

</tr>

<tr>

<td width=>

</td>

<td width=>

</td>

<td width=>

商品名称</td>

<td width=>

<asp:TextBox ID=txtName runat=server Text=<%# Bind(Name) %>></asp:TextBox>

</td>

</tr>

<tr>

<td width=>

</td>

<td width=>

</td>

<td width=>

商品价格</td>

<td width=>

<asp:TextBox ID=txtPrice runat=server Text=<%# Bind(Price) %>></asp:TextBox>

</td>

</tr>

<tr>

<td width=>

</td>

<td width=>

</td>

<td width=>

商品描述</td>

<td width=>

<asp:TextBox ID=txtDescn runat=server Text=<%# Bind(Descn) %>></asp:TextBox>

</td>

</tr>

<tr>

<td width=>

</td>

<td width=>

</td>

<td width=>

供应时间</td>

<td width=>

<asp:TextBox ID=txtSupplyTime runat=server Text=<%# Bind(SupplyTime) %>></asp:TextBox>

</td>

</tr>

<tr>

<td width=>

</td>

<td width=>

</td>

<td width=>

供应日期</td>

<td width=>

<asp:TextBox ID=txtSupplyDate runat=server Text=<%# Bind(SupplyDate) %>></asp:TextBox>

</td>

</tr>

<tr>

<td width=>

</td>

<td width=>

</td>

<td width=>

供应地区</td>

<td width=>

<asp:TextBox ID=txtSupplyArea runat=server Text=<%# Bind(SupplyArea) %>></asp:TextBox>

</td>

</tr>

<tr>

<td height= width=>

</td>

<td height= width=>

</td>

<td height= width=>

</td>

<td height= width=>

</td>

<td height= width= align=right>

<asp:LinkButton ID=lbtnDelete runat=server CommandName=Update Text=更新 />

<asp:LinkButton ID=lbtnNew runat=server CommandName=Cancel Text=取消 />

</td>

</tr>

</table>

</EditItemTemplate>

ItemManageControlascx的后台代码

代码页的主任是读取前台窗体的值主要是FileUpload控件和DropDownList控件的值两个值分别使用了ViewState存储了信息读取数据的时候注意如果用户没有做修改刚使用原来的值这个值放在了privatestaticIList<ItemDetails> itemdetails =newList<ItemDetails>()

[csharp] using System;

using SystemWeb;

using SystemWebUIWebControls;

using SystemCollectionsGeneric;

using WestGardenDAL;

using WestGardenModel;

namespace WestGardenWeb

{

public partial class ItemManageControl : SystemWebUIUserControl

{

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 (eNewMode)

{

case FormViewModeEdit:

thisfvwItemDetailsChangeMode(FormViewModeEdit)

BindFormView()

DropDownList ddl = (DropDownList)fvwItemDetailsFindControl(ddlCategories

BindDropDownList(ddl)

break;

case FormViewModeReadOnly:

thisfvwItemDetailsChangeMode(FormViewModeReadOnly)

BindFormView()

break;

default:

break;

}

}

protected void fvwItemDetails_PreRender(object sender EventArgs e)

{

}

protected void ddlCategories_SelectedIndexChanged(object sender EventArgs e)

{

DropDownList ddl = (DropDownList)fvwItemDetailsFindControl(ddlCategories

ViewState[SelectedCategoryId] = ddlSelectedValue;

}

protected void btnUpload_Click(object sender EventArgs e)

{

FileUpload fup = (FileUpload)fvwItemDetailsFindControl(fupImage

if (fupHasFile)

{

fupSaveAs(ServerMapPath(~/Images/Items/) + fupFileName)

Image img = (Image)fvwItemDetailsFindControl(imgItem

imgImageUrl = ~/Images/Items/ + fupFileNameToString()

ViewState[ImageUrl] = ~/Images/Items/ + fupFileNameToString()

}

else

{

ResponseWrite(<script>alert(请先浏览并选择图片)</script>

}

}

protected void fvwItemDetails_ItemUpdating(object sender FormViewUpdateEventArgs e)

{

if (ViewState[ImageUrl] != null)

{

itemdetails[]Image = ViewState[ImageUrl]ToString()

}

if (ViewState[SelectedCategoryId] != null)

{

DropDownList ddl = (DropDownList)fvwItemDetailsFindControl(ddlCategories

itemdetails[]CategoryId = ViewState[SelectedCategoryId]ToString()

}

TextBox txtname = (TextBox)fvwItemDetailsFindControl(txtName

itemdetails[]Name = txtnameText;

TextBox txtPrice = (TextBox)fvwItemDetailsFindControl(txtPrice

itemdetails[]Price = decimalParse(txtPriceText)

TextBox txtDescn = (TextBox)fvwItemDetailsFindControl(txtDescn

itemdetails[]Descn = txtDescnText;

TextBox txtSupplyTime = (TextBox)fvwItemDetailsFindControl(txtSupplyTime

itemdetails[]SupplyTime = txtSupplyTimeText;

TextBox txtSupplyDate = (TextBox)fvwItemDetailsFindControl(txtSupplyDate

itemdetails[]SupplyDate = txtSupplyDateText;

TextBox txtSupplyArea = (TextBox)fvwItemDetailsFindControl(txtSupplyArea

itemdetails[]SupplyArea = txtSupplyAreaText;

Item item = new Item()

itemUpdateItem(itemdetails[])

fvwItemDetailsChangeMode(FormViewModeReadOnly)

BindFormView()

ViewState[ImageUrl] = null;

ViewState[SelectedCategoryId] = null;

}

private void BindFormView()

{

int itemKey = intParse(RequestQueryString[itemId])

Item item = new Item()

itemdetails = itemGetItemDetailsByItemId(itemKey)

fvwItemDetailsDataSource = itemdetails;

fvwItemDetailsDataBind()

}

private void BindDropDownList(DropDownList ddl)

{

ddlDataSource = new Category()GetCategories()

ddlDataTextField = Name;

ddlDataValueField = CategoryId;

ddlDataBind()

string selectcategory = RequestQueryString[categoryId]ToString()

if (selectcategory != null)

{

ListItem selectedItem = ddlItemsFindByValue(selectcategory)

if (selectedItem != null)

selectedItemSelected = true;

}

}

}

}

using System;

using SystemWeb;

using SystemWebUIWebControls;

using SystemCollectionsGeneric;

using WestGardenDAL;

using WestGardenModel;

namespace WestGardenWeb

{

public partial class ItemManageControl : SystemWebUIUserControl

{

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 (eNewMode)

{

case FormViewModeEdit:

thisfvwItemDetailsChangeMode(FormViewModeEdit)

BindFormView()

DropDownList ddl = (DropDownList)fvwItemDetailsFindControl(ddlCategories

BindDropDownList(ddl)

break;

case FormViewModeReadOnly:

thisfvwItemDetailsChangeMode(FormViewModeReadOnly)

BindFormView()

break;

default:

break;

}

}

protected void fvwItemDetails_PreRender(object sender EventArgs e)

{

}

protected void ddlCategories_SelectedIndexChanged(object sender EventArgs e)

{

DropDownList ddl = (DropDownList)fvwItemDetailsFindControl(ddlCategories

ViewState[SelectedCategoryId] = ddlSelectedValue;

}

protected void btnUpload_Click(object sender EventArgs e)

{

FileUpload fup = (FileUpload)fvwItemDetailsFindControl(fupImage

if (fupHasFile)

{

fupSaveAs(ServerMapPath(~/Images/Items/) + fupFileName)

Image img = (Image)fvwItemDetailsFindControl(imgItem

imgImageUrl = ~/Images/Items/ + fupFileNameToString()

ViewState[ImageUrl] = ~/Images/Items/ + fupFileNameToString()

}

else

{

ResponseWrite(<script>alert(请先浏览并选择图片)</script>

}

}

protected void fvwItemDetails_ItemUpdating(object sender FormViewUpdateEventArgs e)

{

if (ViewState[ImageUrl] != null)

{

itemdetails[]Image = ViewState[ImageUrl]ToString()

}

if (ViewState[SelectedCategoryId] != null)

{

DropDownList ddl = (DropDownList)fvwItemDetailsFindControl(ddlCategories

itemdetails[]CategoryId = ViewState[SelectedCategoryId]ToString()

}

TextBox txtname = (TextBox)fvwItemDetailsFindControl(txtName

itemdetails[]Name = txtnameText;

TextBox txtPrice = (TextBox)fvwItemDetailsFindControl(txtPrice

itemdetails[]Price = decimalParse(txtPriceText)

TextBox txtDescn = (TextBox)fvwItemDetailsFindControl(txtDescn

itemdetails[]Descn = txtDescnText;

TextBox txtSupplyTime = (TextBox)fvwItemDetailsFindControl(txtSupplyTime

itemdetails[]SupplyTime = txtSupplyTimeText;

TextBox txtSupplyDate = (TextBox)fvwItemDetailsFindControl(txtSupplyDate

itemdetails[]SupplyDate = txtSupplyDateText;

TextBox txtSupplyArea = (TextBox)fvwItemDetailsFindControl(txtSupplyArea

itemdetails[]SupplyArea = txtSupplyAreaText;

Item item = new Item()

itemUpdateItem(itemdetails[])

fvwItemDetailsChangeMode(FormViewModeReadOnly)

BindFormView()

ViewState[ImageUrl] = null;

ViewState[SelectedCategoryId] = null;

}

private void BindFormView()

{

int itemKey = intParse(RequestQueryString[itemId])

Item item = new Item()

itemdetails = itemGetItemDetailsByItemId(itemKey)

fvwItemDetailsDataSource = itemdetails;

fvwItemDetailsDataBind()

}

private void BindDropDownList(DropDownList ddl)

{

ddlDataSource = new Category()GetCategories()

ddlDataTextField = Name;

ddlDataValueField = CategoryId;

ddlDataBind()

string selectcategory = RequestQueryString[categoryId]ToString()

if (selectcategory != null)

{

ListItem selectedItem = ddlItemsFindByValue(selectcategory)

if (selectedItem != null)

selectedItemSelected = true;

}

}

}

}

数据访问层DAL中的Itemcs类中添加更新函数UpdateItem()代码如下

[csharp] public void UpdateItem(ItemDetails item)

{

SqlParameter[] parms;

parms = new SqlParameter[]

{

new SqlParameter(@ItemIdSqlDbTypeInt)

new SqlParameter(@CategoryIdSqlDbTypeVarChar

new SqlParameter(@NameSqlDbTypeVarChar

new SqlParameter(@PriceSqlDbTypeDecimal

new SqlParameter(@ImageSqlDbTypeVarChar

new SqlParameter(@DescnSqlDbTypeVarChar

new SqlParameter(@SupplyTimeSqlDbTypeVarChar

new SqlParameter(@SupplyDateSqlDbTypeVarChar

new SqlParameter(@SupplyAreaSqlDbTypeVarChar

};

parms[]Value = itemItemId;

parms[]Value = itemCategoryId;

parms[]Value = itemName;

parms[]Value = itemPrice;

parms[]Value = itemImage;

parms[]Value = itemDescn;

parms[]Value = itemSupplyTime;

parms[]Value = itemSupplyDate;

parms[]Value = itemSupplyArea;

SqlHelperExecuteNonQuery(SqlHelperConnectionStringLocalTransaction CommandTypeText SQL_UPDATE_ITEM parms)

}

上一篇:借助封装类实现线程调用带参方法

下一篇:DevExpress的DateEdit控件显示日期的周名