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

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

程序员文章站 2022-07-07 23:36:02
二、使用 FormView控件编辑数据 1、编辑EditItemTemplate模板,代码如下: [html] &...
FormView显示、更新、插入、删除数据库操作[ASP.NET源代码](二)


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



摘自 ASP.NET技术交流