当前位置:编程学习 > asp >>

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.aspx.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="http://www.w3.org/1999/xhtml"> 
<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"> 
                 &

补充:Web开发 , ASP.Net ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,