FormView显示、更新、插入、删除数据库操作[ASP.NET源代码](三)
三、使用 formview控件更新数据
1、编辑insertitemtemplate模板,代码如下:
[html] <insertitemtemplate>
<table border="0" cellpadding="0" cellspacing="0" width="420">
<tr>
<td colspan="6" height="30" width="420" align="center">
<h4>formview insertitemtemplate 模板</h4>
</td>
</tr>
<tr>
<td width="30">
</td>
<td rowspan="4" width="120">
<:image id="imgitem" runat="server" width="120px" height="120px" imageurl='<%# eval("image") %>'
alternatetext="上传浏览图片" /></td>
<td width="30">
</td>
<td colspan="2">
<asp:fileupload id="fupimage" runat="server" width="100%"/></td>
<td width="60">
<asp:button id="btnupload" text="上传" onclick="btnupload_click" runat="server"></asp:button></td>
</tr>
<tr>
<td width="30">
</td>
<td width="30">
</td>
<td width="60">
分类:</td>
<td width="120">
<asp:dropdownlist id="ddlcategory" runat="server" datasourceid="sdscategory" datatextfield="name"
datavaluefield="categoryid">
</asp:dropdownlist></td>
<td width="60">
</td>
</tr>
<tr>
<td width="30">
</td>
<td width="30">
</td>
<td width="60">
名称:</td>
<td width="120">
<asp:textbox id="txtname" text='<%# bind("name") %>' runat="server" /></td>
<td width="60">
</td>
</tr>
<tr>
<td width="30">
</td>
<td width="30">
</td>
<td width="60">
价格:
</td>
<td width="120">
<asp:textbox id="txtprice" text='<%# bind("price") %>' runat="server" /></td>
<td width="60">
</td>
</tr>
<tr>
<td height="30" width="30">
</td>
<td height="30" width="120">
</td>
<td height="30" width="30">
</td>
<td height="30" width="60">
</td>
<td height="30" width="120" align="right">
<asp:button id="btninsert" text="插入" commandname="insert" runat="server" /></td>
<td height="30" width="60">
<asp:button id="btncancel" text="取消" commandname="cancel" runat="server" /></td>
</tr>
</table>
</insertitemtemplate>
</asp:formview>
<asp:sqldatasource id="sdsitem" runat="server" connectionstring="<%$ connectionstrings:netshopconnstring %>"
selectcommand="select item.itemid as itemid,item.categoryid as categoryid,item.name as name,item.price as price,item.image as image,category.name as categoryname from item inner join category on item.categoryid=category.categoryid"
updatecommand="update item set categoryid=@categoryid,name=@name,price=@price,image=@image where itemid=@itemid"
insertcommand="insert into item(categoryid,name,price,image) values (@categoryid,@name,@price,@image)">
<updateparameters>
<asp:parameter name="categoryid" />
<asp:parameter name="name" />
<asp:parameter name="price" />
<asp:parameter name="image" />
<asp:parameter name="itemid" />
</updateparameters>
<insertparameters>
<asp:parameter name="categoryid" />
<asp:parameter name="name" />
<asp:parameter name="price" />
<asp:parameter name="image" />
</insertparameters>
<insertitemtemplate>
<table border="0" cellpadding="0" cellspacing="0" width="420">
<tr>
<td colspan="6" height="30" width="420" align="center">
<h4>formview insertitemtemplate 模板</h4>
</td>
</tr>
<tr>
<td width="30">
</td>
<td rowspan="4" width="120">
<asp:image id="imgitem" runat="server" width="120px" height="120px" imageurl='<%# eval("image") %>'
alternatetext="上传浏览图片" /></td>
<td width="30">
</td>
<td colspan="2">
<asp:fileupload id="fupimage" runat="server" width="100%"/></td>
<td width="60">
<asp:button id="btnupload" text="上传" onclick="btnupload_click" runat="server"></asp:button></td>
</tr>
<tr>
<td width="30">
</td>
<td width="30">
</td>
<td width="60">
分类:</td>
<td width="120">
<asp:dropdownlist id="ddlcategory" runat="server" datasourceid="sdscategory" datatextfield="name"
datavaluefield="categoryid">
</asp:dropdownlist></td>
<td width="60">
</td>
</tr>
<tr>
<td width="30">
</td>
<td width="30">
</td>
<td width="60">
名称:</td>
<td width="120">
<asp:textbox id="txtname" text='<%# bind("name") %>' runat="server" /></td>
<td width="60">
</td>
</tr>
<tr>
<td width="30">
</td>
<td width="30">
</td>
<td width="60">
价格:
</td>
<td width="120">
<asp:textbox id="txtprice" text='<%# bind("price") %>' runat="server" /></td>
<td width="60">
</td>
</tr>
<tr>
<td height="30" width="30">
</td>
<td height="30" width="120">
</td>
<td height="30" width="30">
</td>
<td height="30" width="60">
</td>
<td height="30" width="120" align="right">
<asp:button id="btninsert" text="插入" commandname="insert" runat="server" /></td>
<td height="30" width="60">
<asp:button id="btncancel" text="取消" commandname="cancel" runat="server" /></td>
</tr>
</table>
</insertitemtemplate>
</asp:formview>
<asp:sqldatasource id="sdsitem" runat="server" connectionstring="<%$ connectionstrings:netshopconnstring %>"
selectcommand="select item.itemid as itemid,item.categoryid as categoryid,item.name as name,item.price as price,item.image as image,category.name as categoryname from item inner join category on item.categoryid=category.categoryid"
updatecommand="update item set categoryid=@categoryid,name=@name,price=@price,image=@image where itemid=@itemid"
insertcommand="insert into item(categoryid,name,price,image) values (@categoryid,@name,@price,@image)">
<updateparameters>
<asp:parameter name="categoryid" />
<asp:parameter name="name" />
<asp:parameter name="price" />
<asp:parameter name="image" />
<asp:parameter name="itemid" />
</updateparameters>
<insertparameters>
<asp:parameter name="categoryid" />
<asp:parameter name="name" />
<asp:parameter name="price" />
<asp:parameter name="image" />
</insertparameters>
2、这个模板和编辑模板基本一样,就是点击“新建”按钮进入时,没有绑定数据而已,因此,“上传”按钮的响应函数可复用,更新前的赋值操作也基本是一样的。
为fvwitem添加响应函数,代码如下:
[csharp] protected void fvwitem_iteminserting(object sender, formviewinserteventargs e)
{
dropdownlist ddl = (dropdownlist)fvwitem.findcontrol("ddlcategory");
sdsitem.insertparameters["categoryid"].defaultvalue = ddl.selectedvalue;
image img = (image)fvwitem.findcontrol("imgitem");
sdsitem.insertparameters["image"].defaultvalue = img.imageurl;
}
protected void fvwitem_iteminserting(object sender, formviewinserteventargs e)
{
dropdownlist ddl = (dropdownlist)fvwitem.findcontrol("ddlcategory");
sdsitem.insertparameters["categoryid"].defaultvalue = ddl.selectedvalue;
image img = (image)fvwitem.findcontrol("imgitem");
sdsitem.insertparameters["image"].defaultvalue = img.imageurl;
}
3、别忘了添加fvwitem的insertcommand命令,并添加参数变量updateparameters:
[html] insertcommand="insert into item(categoryid,name,price,image) values (@categoryid,@name,@price,@image)"
insertcommand="insert into item(categoryid,name,price,image) values (@categoryid,@name,@price,@image)"
[html] <asp:sqldatasource id="sdscategory" runat="server" connectionstring="<%$ connectionstrings:netshopconnstring %>"
selectcommand="select categoryid,name from category">
</asp:sqldatasource>
<asp:sqldatasource id="sdscategory" runat="server" connectionstring="<%$ connectionstrings:netshopconnstring %>"
selectcommand="select categoryid,name from category">
</asp:sqldatasource>
4、在中查看运行结果。
四、使用 formview控件删除数据
这个操作不需要参数,所了也就最简单了,只要在sdsitem中添加一个deletecommand="delete from item where(itemid=@itemid)"命令就可以了。
为了在删除中的图片地址的同时,也删除服务器端的图片文件,还是添加了一个消息响应函数,代码如下:
[csharp] protected void fvwitem_itemdeleting(object sender, formviewdeleteeventargs e)
{
image img = (image)fvwitem.findcontrol("imgitem");
file.delete(server.mappath(img.imageurl));
}
protected void fvwitem_itemdeleting(object sender, formviewdeleteeventargs e)
{
image img = (image)fvwitem.findcontrol("imgitem");
file.delete(server.mappath(img.imageurl));
}
protected void fvwitem_itemdeleting(object sender,formviewdeleteeventargs e)
{
image img = (image)fvwitem.findcontrol("imgitem");
file.delete(server.mappath(img.imageurl));
}
摘自 asp.net技术交流
上一篇: 变量命名的方法及使用
下一篇: node环境安装教程