网上购物系统(Task009)——FormView插入删除商品详细信息
一、进入插入模板
1、protectedvoid fvwItemDetails_ModeChanging(object sender,FormViewModeEventArgs e)函数中添加代码:
case FormViewMode.Insert:
this.fvwItemDetails.ChangeMode(FormViewMode.Insert);
break;
2、此时,可进入插入模板,不过,不显示任何信息,也不能够获得下拉列表框的句柄,须添加PreRender()消息响应函数,在这个消息响应函数中添加填充下拉列表框的代码:
protected void fvwItemDetails_PreRender(object sender,EventArgs e)
{
if (fvwItemDetails.CurrentMode ==FormViewMode.Insert)
{
DropDownList ddl = (DropDownList)fvwItemDetails.FindControl("ddlCategories");
if (ddl != null)
{
BindDropDownList(ddl);
}
}
}
二、修改BindDropDownList()函数
[csharp] private void BindDropDownList(DropDownList ddl)
{
ddl.DataSource = new Category().GetCategories();
ddl.DataTextField = "Name";
ddl.DataValueField = "CategoryId";
ddl.DataBind();
if (ViewState["SelectedCategoryId"] != null)
{
ListItem selectedItem = ddl.Items.FindByValue(ViewState["SelectedCategoryId"].ToString());
if (selectedItem != null)
selectedItem.Selected = true;
}
else
{
string selectcategory = Request.QueryString["categoryId"].ToString();
ListItem selectedItem = ddl.Items.FindByValue(selectcategory);
if (selectedItem != null)
selectedItem.Selected = true;
}
}
private void BindDropDownList(DropDownList ddl)
{
ddl.DataSource = new Category().GetCategories();
ddl.DataTextField = "Name";
ddl.DataValueField = "CategoryId";
ddl.DataBind();
if (ViewState["SelectedCategoryId"] != null)
{
ListItem selectedItem = ddl.Items.FindByValue(ViewState["SelectedCategoryId"].ToString());
if (selectedItem != null)
selectedItem.Selected = true;
}
else
{
string selectcategory = Request.QueryString["categoryId"].ToString();
ListItem selectedItem = ddl.Items.FindByValue(selectcategory);
if (selectedItem != null)
selectedItem.Selected = true;
}
}
三、添加消息响应函数fvwItemDetails_ItemInserting()
[csharp] protected void fvwItemDetails_ItemInserting(object sender, FormViewInsertEventArgs e)
{
ItemDetails itemdetails = new ItemDetails();
if (ViewState["ImageUrl"] != null)
{
itemdetails.Image = ViewState["ImageUrl"].ToString();
}
if (ViewState["SelectedCategoryId"] != null)
{
DropDownList ddl = (DropDownList)fvwItemDetails.FindControl("ddlCategories");
itemdetails.CategoryId = ViewState["SelectedCategoryId"].ToString();
}
TextBox txtname = (TextBox)fvwItemDetails.FindControl("txtName");
itemdetails.Name = txtname.Text;
TextBox txtPrice = (TextBox)fvwItemDetails.FindControl("txtPrice");
itemdetails.Price = decimal.Parse(txtPrice.Text);
TextBox txtDescn = (TextBox)fvwItemDetails.FindControl("txtDescn");
itemdetails.Descn = txtDescn.Text;
TextBox txtSupplyTime = (TextBox)fvwItemDetails.FindControl("txtSupplyTime");
itemdetails.SupplyTime = txtSupplyTime.Text;
TextBox txtSupplyDate = (TextBox)fvwItemDetails.FindControl("txtSupplyDate");
itemdetails.SupplyDate = txtSupplyDate.Text;
TextBox txtSupplyArea = (TextBox)fvwItemDetails.FindControl("txtSupplyArea");
itemdetails.SupplyArea = txtSupplyArea.Text;
Item item = new Item();
item.InsertItem(itemdetails);
fvwItemDetails.ChangeMode(FormViewMode.ReadOnly);
BindFormView();
ViewState["ImageUrl"] = null;
ViewState["SelectedCategoryId"] = null;
}
protected void fvwItemDetails_ItemInserting(object sender, FormViewInsertEventArgs e)
{
ItemDetails itemdetails = new ItemDetails();
if (ViewState["ImageUrl"] != null)
{
itemdetails.Image = ViewState["ImageUrl"].ToString();
}
if (ViewState["SelectedCategoryId"] != null)
{
DropDownList ddl = (DropDownList)fvwItemDetails.FindControl("ddlCategories");
itemdetails.CategoryId = ViewState["SelectedCategoryId"].ToString();
}
TextBox txtname = (TextBox)fvwItemDetails.FindControl("txtName");
itemdetails.Name = txtname.Text;
TextBox txtPrice = (TextBox)fvwItemDetails.FindControl("txtPrice");
itemdetails.Price = decimal.Parse(txtPrice.Text);
TextBox txtDescn = (TextBox)fvwItemDetails.FindControl("txtDescn");
itemdetails.Descn = txtDescn.Text;
TextBox txtSupplyTime = (TextBox)fvwItemDetails.FindControl("txtSupplyTime");
itemdetails.SupplyTime = txt
补充:Web开发 , ASP.Net ,