欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-13 09:26:29
  formview可分页呈现一个表格的数据,每页只呈现表格中的一项。它的最大特点是可*编辑模板,一般用来显示商品的详细信息。formview有三个可编辑模板,itemt...
 
FormView显示、更新、插入、删除数据库操作[ASP.NET源代码](一) FormView显示、更新、插入、删除数据库操作[ASP.NET源代码](一)
FormView显示、更新、插入、删除数据库操作[ASP.NET源代码](一) 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" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> 
<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技术交流