• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

源代码:13033480群共享

三、使用 FormView控件更新数据

1、编辑InsertItemTemplate模板,代码如下:

<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添加响应函数,代码如下:

 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

InsertCommand="INSERT INTO Item(CategoryId,Name,Price,Image) VALUES (@CategoryId,@Name,@Price,@Image)"


 

<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)"命令就可以了。

为了在删除数据库中的图片地址的同时,也删除服务器端的图片文件,还是添加了一个消息响应函数,代码如下:

    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));

}

 

 

参考网址:http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.formview%28v=VS.80%29.aspx


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ASP.Net生成静态HTML页[转载]发布时间:2022-07-10
下一篇:
Asp.NetCore轻松学-10分钟使用EFCore连接MSSQL数据库发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap