当前位置: 移动技术网 > IT编程>开发语言>.net > FormView显示、更新、插入、删除数据库操作[ASP.NET源代码](一)

FormView显示、更新、插入、删除数据库操作[ASP.NET源代码](一)

2018年12月07日  | 移动技术网IT编程  | 我要评论

异能剑圣,让青春继续第二季,我家天使与恶魔

 
formview可分页呈现一个表格的数据,每页只呈现表格中的一项。它的最大特点是可自由编辑模板,一般用来显示商品的详细信息。formview有三个可编辑模板,itemtemplate、edititemtemplate和insertitemtemplate、常用来管理表格数据,显示、编辑、插入、删除表格中的数据项。
一、使用 formview控件显示 sqldatasource控件中的值


1、设置formview控件,注意datakeynames="itemid"项。
2、设置sqldatasource属性,由于要查询两个内联表,两个表中都有一个name字段,因此用了别名。
3、编辑itemtemplate模板,先添加了“编辑”、“删除”、“新建”按钮,“编辑”和“新建”按钮都有个commandname属性,分别为edit和new,点击可分别进入edititemtemplate和insertitemtemplate、模板;删除按钮的commandname属性是delete,点击可执行sqldatasource中的deletecommand命令,同时,还可发出onitemdeleting等命令,在删除前完成一些功能。
4、窗体文件代码如下:
[html]
<%@ page language="c#" autoeventwireup="true" codefile="formviewdemo1.x.cs" inherits="formviewdemo1" %> 
 
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" ""> 
<html xmlns=""> 
<head runat="server"> 
    <title>肯德基订餐</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
        <h3>formview 显示、更新、插入、删除数据库操作</h3> 
        <asp:formview id="fvwitem" datasourceid="sdsitem" runat="server"  
            allowpaging="true" 
            datakeynames="itemid"  
            emptydatatext="数据库中暂时没有任何数据"> 
             
            <rowstyle backcolor="yellow" wrap="false" /> 
            <insertrowstyle backcolor="greenyellow" wrap="false" /> 
            <editrowstyle backcolor="lightpink" wrap="false" /> 
             
            <itemtemplate> 
                <table border="0" cellpadding="0" cellspacing="0" width="420"> 
                    <tr> 
                        <td colspan="6" height="30" width="420" align="center"> 
                        <h4>formview itemtemplate 模板</h4> 
                        </td> 
                    </tr> 
                    <tr> 
                        <td width="30"> 
                        </td> 
                        <td rowspan="4" width="120"> 
                            <asp:image width="120" height="120" id="imgitem" imageurl='<%# eval("image") %>' alternatetext='<%# eval("name") %>' 
                                runat="server" /></td> 
                        <td width="30"> 
                        </td> 
                        <td width="60"> 
                        </td> 
                        <td width="60"> 
                        </td> 
                        <td width="60"> 
                        </td> 
                        <td width="60"> 
                        </td> 
                    </tr> 
                    <tr> 
                        <td width="30"> 
                        </td> 
                        <td width="30"> 
                        </td> 
                        <td width="60"> 
                            类别:</td> 
                        <td colspan="2"> 
                        <%# eval("categoryname") %> 
                        </td> 
                        <td width="60"> 
                        </td> 
                    </tr> 
                    <tr> 
                        <td width="30"> 
                        </td> 
                        <td width="30"> 
                        </td> 
                        <td width="60"> 
                            名称:</td> 
                        <td colspan="2"> 
                        <%# eval("name") %> 
                        </td> 
                        <td width="60"> 
                        </td> 
                    </tr> 
                    <tr> 
                        <td width="30"> 
                        </td> 
                        <td width="30"> 
                        </td> 
                        <td width="60"> 
                            价格: 
                        </td> 
                        <td colspan="2"> 
                        <%# eval("price") %> 
                        </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="60"> 
                            <asp:button id="btnedit" runat="server" text="编辑" commandname="edit"/></td> 
                        <td height="30" width="60"> 
                            <asp:button id="btndelete" runat="server" text="删除" commandname="delete"/></td> 
                        <td height="30" width="60"> 
                            <asp:button id="btnnew" runat="server" text="新建" commandname="new" /></td> 
                    </tr> 
                </table> 
            </itemtemplate> 
        </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"> 
        </asp:sqldatasource> 
    </form> 
</body> 
</html> 

 
5、代码页不需要任何代码,可直接在查看运行情图,如图1示。

 

摘自 asp.net技术交流

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网