zoukankan      html  css  js  c++  java
  • FormView 显示、更新、插入、删除数据库操作[ASP.NET源代码](二)

    源代码:13033480群共享

    二、使用 FormView控件编辑数据

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

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

    UpdateCommand="UPDATE Item SET CategoryId=@CategoryId,Name=@Name,Price=@Price,Image=@Image WHERE ItemId=@ItemId"

    <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按钮,并添加了这个按钮的响应函数,点击后,可将文件上传,并在窗体中显示上传后的图片,代码如下:

        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,配置如下:

    <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"消息响应函数,代码如下:

        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、在浏览器中查看运行结果。

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

  • 相关阅读:
    node.js----服务器http
    node.js---对文件操作
    node.js
    历史管理
    h5
    git与github
    js中面向对象(创建对象的几种方式)
    jq基础
    POJ 2492 A Bug's Life
    POJ 1742 Coins
  • 原文地址:https://www.cnblogs.com/WestGarden/p/3138438.html
Copyright © 2011-2022 走看看