分享

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

 悟静 2012-11-11
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./TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www./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"> 
                        </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示。

\


二、使用 FormView控件编辑数据
1、编辑EditItemTemplate模板,代码如下:
[html]
<EditItemTemplate> 
                <table border="0" cellpadding="0" cellspacing="0" width="420"> 
                    <tr> 
                        <td colspan="6" height="30" width="420" align="center"> 
                        <h4>FormView EditItemTemplate 模板</h4> 
                        </td> 
                    </tr> 
                    <tr> 
                        <td width="30"> 
                        </td> 
                        <td rowspan="4" width="120"> 
                            <asp:Image ID="imgItem" runat="server" AlternateText="上传浏览图片" Height="120px" ImageUrl='<%# Eval("Image") %>' 
                                Width="120px" /></td> 
                        <td width="30"> 
                        </td> 
                        <td colspan="2"> 
                            <asp:FileUpload ID="fupImage" runat="server" Width="100%" /></td> 
                        <td width="60"> 
                            <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="上传" /></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" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox></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" runat="server" Text='<%# Bind("Price") %>'></asp:TextBox></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 align="right" height="30" width="120"> 
                            <asp:Button ID="btnUpdate" runat="server" CommandName="Update" Text="更新" /></td> 
                        <td height="30" width="60"> 
                            <asp:Button ID="btnCancel" runat="server" CommandName="Cancel" Text="取消" /></td> 
                    </tr> 
                </table> 
            </EditItemTemplate> 

 
2、为SqlDataSource控件sdsItem添加UpdateCommand命令,并添加<UpdateParameters>
[html]
UpdateCommand="UPDATE Item SET CategoryId=@CategoryId,Name=@Name,Price=@Price,Image=@Image WHERE ItemId=@ItemId

[html]
<UpdateParameters> 
    <asp:Parameter Name="CategoryId" /> 
    <asp:Parameter Name="Name" /> 
    <asp:Parameter Name="Price" /> 
    <asp:Parameter Name="Image" /> 
    <asp:Parameter Name="ItemId" /> 
</UpdateParameters> 

3、编辑模板加了一个FileUpload控件,可以选择并上传图片图片,为了能在上传后显示图片,添加了一个btnUpload按钮,并添加了这个按钮的响应函数,点击后,可将文件上传,并在窗体中显示上传后的图片,代码如下:
[csharp]
protected void btnUpload_Click(object sender, EventArgs e) 

    FileUpload fup = (FileUpload)fvwItem.FindControl("fupImage"); 
 
    if (fup.HasFile) 
    { 
        fup.SaveAs(Server.MapPath("~\\Images\\Items\\") + fup.FileName); 
 
        String str = "~\\Images\\Items\\" + fup.FileName.ToString(); 
        Image img = (Image)fvwItem.FindControl("imgItem"); 
        img.ImageUrl = str; 
    } 
    else 
    { 
        Response.Write("<script>alert('请先浏览并选择图片')</script>"); 
    } 

 4、在模板中添加一个类别下拉列表框,为了获得一个完全的类别,只能再弄一个SqlDateSource,配置如下:
[html]
<asp:SqlDataSource ID="sdsCategory" runat="server" ConnectionString="<%$ ConnectionStrings:NetShopConnString %>" 
    SelectCommand="SELECT CategoryId,Name FROM Category"> 
</asp:SqlDataSource> 

5、5、编辑模板中,CategoryID和Image等参数没有双向绑定,需要在上传前给这两个参数赋值,为些,为fvwItem添加了OnItemUpdating="fvwItem_ItemUpdating"消息响应函数,代码如下:
[csharp]
protected void fvwItem_ItemUpdating(object sender, FormViewUpdateEventArgs e) 

    DropDownList ddl = (DropDownList)fvwItem.FindControl("ddlCategory"); 
    sdsItem.UpdateParameters["CategoryId"].DefaultValue = ddl.SelectedValue; 
 
    Image img = (Image)fvwItem.FindControl("imgItem"); 
    sdsItem.UpdateParameters["Image"].DefaultValue = img.ImageUrl; 
 

6、在浏览器中查看运行结果。

\


三、使用 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"> 
                            <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> 
<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));

}



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多