网上购物系统(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 = 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;
}
四、在数据访问层dal的item.cs类中,添加insertitem(itemdetails item)函数
[csharp] public void insertitem(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_insert_item, parms);
}
public void insertitem(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_insert_item, parms);
}
五、formview删除详细信息比较简单,不过,步骤和前面是一样的。
1、添加消息响应函数
[csharp] protected void fvwitemdetails_itemdeleting(object sender, formviewdeleteeventargs e)
{
itemdetails itemdetails = new itemdetails();
itemdetails.itemid = int.parse(request.querystring["itemid"]);
item item = new item();
item.deleteitem(itemdetails);
image img = (image)fvwitemdetails.findcontrol("imgitem");
file.delete(server.mappath(img.imageurl));
bindformview();
}
protected void fvwitemdetails_itemdeleting(object sender, formviewdeleteeventargs e)
{
itemdetails itemdetails = new itemdetails();
itemdetails.itemid = int.parse(request.querystring["itemid"]);
item item = new item();
item.deleteitem(itemdetails);
image img = (image)fvwitemdetails.findcontrol("imgitem");
file.delete(server.mappath(img.imageurl));
bindformview();
}
在删除数据的同时,删除了服务器端文件。
2、在数据访问层dal的item.cs类中,添加deleteitem(itemdetails item)函数
[csharp] public void deleteitem(itemdetails item)
{
sqlparameter[] parms;
parms = new sqlparameter[]
{
new sqlparameter("@itemid",sqldbtype.int)
};
parms[0].value = item.itemid;
sqlhelper.executenonquery(sqlhelper.connectionstringlocaltransaction, commandtype.text, sql_delete_item, parms);
}
public void deleteitem(itemdetails item)
{
sqlparameter[] parms;
parms = new sqlparameter[]
{
new sqlparameter("@itemid",sqldbtype.int)
};
parms[0].value = item.itemid;
sqlhelper.executenonquery(sqlhelper.connectionstringlocaltransaction, commandtype.text, sql_delete_item, parms);
}
六、浏览default.x,查看运行结果。
作者 yousuosi
上一篇: 当iPhone6被上升成人民内部矛盾