编辑项目模板中的文件上传

时间:2012-04-04 23:48:53

标签: c# asp.net listview

我希望能够使用列表视图更新数据库中的图像,但我无法做到  文件上传总是给我一个空值,无法解决问题可以任何人帮我在asp.net和c#

初学者

这是我的代码:

    <body>
    <form id="form1" runat="server">
    <div>

    <asp:Panel ID="Panel1" runat="server" Height="1138px">
        &nbsp;&nbsp;&nbsp;
        <br />
        <br />
        <br />
        &nbsp;&nbsp;&nbsp;
        <asp:Label ID="Label1" runat="server" Text="Articles" ForeColor="#FF3300"></asp:Label>
        &nbsp;<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="Name" 
            Height="16px" Width="267px">
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:siteDBConnectionString %>" 
            SelectCommand="SELECT * FROM [Articles]"></asp:SqlDataSource>
        &nbsp;&nbsp;
        <br />
        <br />
        &nbsp;&nbsp;
        <asp:ListView ID="ListView1" runat="server" DataKeyNames="Article_No" 
            DataSourceID="SqlDataSource2" GroupItemCount="3" 
            onselectedindexchanged="ListView1_SelectedIndexChanged">
            <AlternatingItemTemplate>
                <td runat="server" style="background-color: #FAFAD2;color: #284775;">
                    Name:
                    <asp:Label ID="NameLabel" runat="server" 
                        Text='<%# Eval("Name") %>' />
                    <br />
                    Subject:
                    <asp:Label ID="SubjectLabel" runat="server" Text='<%# Eval("Subject") %>' />
                    <br />
                    Date:
                    <asp:Label ID="DateLabel" runat="server" Text='<%# Eval("Date") %>' />
                    <br />
                    Picture:
                    <asp:Label ID="PictureLabel" runat="server" Text='<%# Eval("Picture") %>' />
                    <br />
                    Content:
                    <asp:Label ID="ContentLabel" runat="server" Text='<%# Eval("Content") %>' />
                    <br />
                    Article_No:
                    <asp:Label ID="Article_NoLabel" runat="server" 
                        Text='<%# Eval("Article_No") %>' />
                    <br />
                    <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" 
                        Text="Delete" />
                    <br />
                    <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                    <br />
                </td>
            </AlternatingItemTemplate>
            <EditItemTemplate>
                <td runat="server" style="background-color: #FFCC66;color: #000080;">
                    Name:
                    <asp:TextBox ID="NameTextBox" runat="server" 
                        Text='<%# Bind("Name", "{0}") %>' />
                    <br />
                    Subject:
                    <asp:TextBox ID="SubjectTextBox" runat="server" 
                        Text='<%# Bind("Subject", "{0}") %>' />
                    <br />
                    Date:
                    <asp:TextBox ID="DateTextBox" runat="server" 
                        Text='<%# Bind("Date", "{0:g}") %>' />
                    <br />
                    Picture:
                    <asp:FileUpload ID="fuimage" runat="server"  />
                    <br />
                    Content:
                    <asp:TextBox ID="ContentTextBox" runat="server" 
                        Text='<%# Bind("Content", "{0}") %>' TextMode="MultiLine" />
                    <br />
                    Article_No:
                    <asp:Label ID="Article_NoLabel1" runat="server" Enabled="False" 
                        Text='<%# Eval("Article_No", "{0}") %>' />
                    <br />
                    <asp:Button ID="UpdateButton" runat="server" CommandName="Update" 
                        OnClick="UpdateButton_Click1" Text="Update" />
                    <br />
                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                        Text="Cancel" />
                    <br />
                </td>
            </EditItemTemplate>
            <EmptyDataTemplate>
                <table runat="server" 
                    style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
                    <tr>
                        <td>
                            No data was returned.</td>
                    </tr>
                </table>
            </EmptyDataTemplate>
            <EmptyItemTemplate>
                <td runat="server" />
            </EmptyItemTemplate>
            <GroupTemplate>
                <tr ID="itemPlaceholderContainer" runat="server">
                    <td ID="itemPlaceholder" runat="server">
                    </td>
                </tr>
            </GroupTemplate>
            <InsertItemTemplate>
                <td runat="server" style="">
                    Name:
                    <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
                    <br />
                    Subject:
                    <asp:TextBox ID="SubjectTextBox" runat="server" Text='<%# Bind("Subject") %>' />
                    <br />
                    Date:
                    <asp:TextBox ID="DateTextBox" runat="server" Text='<%# Bind("Date") %>' />
                    <br />
                    Picture:
                    <asp:TextBox ID="PictureTextBox" runat="server" Text='<%# Bind("Picture") %>' />
                    <br />
                    Content:
                    <asp:TextBox ID="ContentTextBox" runat="server" Text='<%# Bind("Content") %>' />
                    <br />
                    <asp:Button ID="InsertButton" runat="server" CommandName="Insert" 
                        Text="Insert" />
                    <br />
                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                        Text="Clear" />
                    <br />
                </td>
            </InsertItemTemplate>
            <ItemTemplate>
                <td runat="server" style="background-color: #FFFBD6;color: #333333;">
                    Name:
                    <asp:Label ID="NameLabel" runat="server" 
                        Text='<%# Eval("Name") %>' />
                    <br />
                    Subject:
                    <asp:Label ID="SubjectLabel" runat="server" Text='<%# Eval("Subject") %>' />
                    <br />
                    Date:
                    <asp:Label ID="DateLabel" runat="server" Text='<%# Eval("Date") %>' />
                    <br />
                    Picture:
                    <asp:Label ID="PictureLabel" runat="server" Text='<%# Eval("Picture") %>' />
                    <br />
                    Content:
                    <asp:Label ID="ContentLabel" runat="server" Text='<%# Eval("Content") %>' />
                    <br />
                    Article_No:
                    <asp:Label ID="Article_NoLabel" runat="server" 
                        Text='<%# Eval("Article_No") %>' />
                    <br />
                    <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" 
                        Text="Delete" />
                    <br />
                    <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                    <br />
                </td>
            </ItemTemplate>
            <LayoutTemplate>
                <table runat="server">
                    <tr runat="server">
                        <td runat="server">
                            <table ID="groupPlaceholderContainer" runat="server" border="1" 
                                style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
                                <tr ID="groupPlaceholder" runat="server">
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr runat="server">
                        <td runat="server" 
                            style="text-align: center;background-color: #FFCC66;font-family: Verdana, Arial, Helvetica, sans-serif;color: #333333;">
                            <asp:DataPager ID="DataPager2" runat="server" PageSize="12">
                                <Fields>
                                    <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
                                        ShowLastPageButton="True" />
                                </Fields>
                            </asp:DataPager>
                        </td>
                    </tr>
                </table>
            </LayoutTemplate>
            <SelectedItemTemplate>
                <td runat="server" 
                    style="background-color: #FFCC66;font-weight: bold;color: #000080;">
                    Name:
                    <asp:Label ID="NameLabel" runat="server" 
                        Text='<%# Eval("Name") %>' />
                    <br />
                    Subject:
                    <asp:Label ID="SubjectLabel" runat="server" Text='<%# Eval("Subject") %>' />
                    <br />
                    Date:
                    <asp:Label ID="DateLabel" runat="server" Text='<%# Eval("Date") %>' />
                    <br />
                    Picture:
                    <asp:Label ID="PictureLabel" runat="server" Text='<%# Eval("Picture") %>' />
                    <br />
                    Content:
                    <asp:Label ID="ContentLabel" runat="server" Text='<%# Eval("Content") %>' />
                    <br />
                    Article_No:
                    <asp:Label ID="Article_NoLabel" runat="server" 
                        Text='<%# Eval("Article_No") %>' />
                    <br />
                    <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" 
                        Text="Delete" />
                    <br />
                    <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                    <br />
                </td>
            </SelectedItemTemplate>
        </asp:ListView>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
            ConnectionString="<%$ ConnectionStrings:siteDBConnectionString %>" 
            DeleteCommand="DELETE FROM [Articles] WHERE [Article_No] = @Article_No" 
            InsertCommand="INSERT INTO [Articles] ([Name], [Subject], [Date], [Picture], [Content]) VALUES (@Name, @Subject, @Date, @Picture, @Content)" 
            SelectCommand="SELECT [Name], [Subject], [Date], [Picture], [Content], [Article_No] FROM [Articles] WHERE ([Name] = @Name)" 

            UpdateCommand="UPDATE [Articles] SET [Name] = @Name, [Subject] = @Subject, [Date] = @Date, [Picture] = @Picture, [Content] = @Content WHERE [Article_No] = @Article_No">
            <DeleteParameters>
                <asp:Parameter Name="Article_No" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="Subject" Type="String" />
                <asp:Parameter Name="Date" Type="DateTime" />
                <asp:Parameter Name="Picture" Type="String" />
                <asp:Parameter Name="Content" Type="String" />
            </InsertParameters>
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="Name" 
                    PropertyName="SelectedValue" Type="String" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="Subject" Type="String" />
                <asp:Parameter Name="Date" Type="DateTime" />
                <asp:Parameter Name="Picture" Type="String" />
                <asp:Parameter Name="Content" Type="String" />
                <asp:Parameter Name="Article_No" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <br />
        &nbsp;
        <br />
        <br />
    </asp:Panel>

</div>
</form>

  这就是我投入的代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.IO;
    using System.Data.SqlClient;

    namespace WebApplication2
    {
     public partial class WebForm4 : System.Web.UI.Page
     {
    SqlConnection cnn = new SqlConnection(@"Data           Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Moeman\My   Documents\siteDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void DateTextBox_TextChanged(object sender, EventArgs e)
    {


    }

    protected void ListView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {

    }



    protected void UpdateButton_Click1(object sender, EventArgs e)
    {
        FileUpload fuimage = (FileUpload)ListView1.FindControl("fuimage");
        String fupload = fuimage.FileName;

        if (fuimage != null && fuimage.HasFile)
        {

            fuimage.SaveAs(Server.MapPath("~\\Images\\" + fupload.Trim()));


            string path = "~\\Images\\" + fupload.Trim();
            cnn.Open();
            SqlCommand cmd = new SqlCommand("update Articles set Picture=@Picture ", cnn);
            if (fuimage.HasFile)
            {
                cmd.Parameters.AddWithValue("@Picture", path);
            }
            else
            {
                cmd.Parameters.AddWithValue("@Picture", "");
            }
            cmd.ExecuteNonQuery();
            cnn.Close();
        }
    }
}

}

1 个答案:

答案 0 :(得分:2)

使用ListView.EditItem来查找FileUpload控件。

FileUpload fuimage = (FileUpload)ListView1.EditItem.FindControl("fuimage");