使用listview和fileupload在db中存储映像路径

时间:2013-08-12 12:10:49

标签: c# asp.net listview file-upload

我想将图像存储在带有列标题,新闻,图像的表格的数据库中。任何人都可以帮我在哪里放置c#代码,即在什么处理程序中,以便它上传到新闻和标题所属的同一列。

请帮助,将是一个真正的大帮助。请

ASPX:

<asp:ListView ID="ListView1" runat="server" DataKeyNames="Id"
        DataSourceID="SqlDataSource1" InsertItemPosition="LastItem">

        <EditItemTemplate>
            <li style="">Id:
                <asp:Label ID="IdLabel1" runat="server" Text='<%# Eval("Id") %>' />
                <br />
                Title:
                <asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title") %>' />
                <br />
                News:
                <asp:TextBox ID="NewsTextBox" runat="server" Text='<%# Bind("News") %>' />
                <br />
                ImageUrl:
                <asp:TextBox ID="ImageUrlTextBox" runat="server"
                    Text='<%# Bind("ImageUrl") %>' />
                <br />
                Date:
                <asp:TextBox ID="DateTextBox" runat="server" Text='<%# Bind("Date") %>' />
                <br />
                <asp:Button ID="UpdateButton" runat="server" CommandName="Update"
                    Text="Update" />
                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
                    Text="Cancel" />
            </li>
        </EditItemTemplate>
        <InsertItemTemplate>
            <li style="">Title:
                <asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title") %>' />
                <br />News:
                <asp:TextBox ID="NewsTextBox" runat="server" Text='<%# Bind("News") %>' />
                <br />

                <asp:FileUpload ID="FileUpload1" runat="server" />

                <br />Date:
                <asp:TextBox ID="DateTextBox" runat="server" Text='<%# Bind("Date") %>' />
                <br />
                <asp:Button ID="InsertButton" runat="server" CommandName="Insert"
                    Text="Insert" />
                <asp:Button ID="CancelButton" CausesValidation="false" runat="server" CommandName="Cancel"
                    Text="Clear" />
            </li>
        </InsertItemTemplate>
        <ItemTemplate>
            <li style="">Id:
                <asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' />
                <br />
                Title:
                <asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' />
                <br />
                News:
                <asp:Label ID="NewsLabel" runat="server" Text='<%# Eval("News") %>' />
                <br />

                <asp:Image ID="ImageUrl" runat="server" ImageUrl='<%# Eval("News") %>' />


                <br />
                Date:
                <asp:Label ID="DateLabel" runat="server" Text='<%# Eval("Date") %>' />
                <br />
                <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                <asp:Button ID="DeleteButton" CausesValidation="false" runat="server" CommandName="Delete"
                    Text="Delete" />
            </li>
        </ItemTemplate>
        <LayoutTemplate>
            <ul ID="itemPlaceholderContainer" runat="server" style="">
                <li runat="server" id="itemPlaceholder" />
            </ul>
            <div style="">
                <asp:DataPager ID="DataPager1" runat="server">
                    <Fields>
                        <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
                            ShowLastPageButton="True" />
                    </Fields>
                </asp:DataPager>
            </div>
        </LayoutTemplate>

    </asp:ListView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        DeleteCommand="DELETE FROM [News] WHERE [Id] = @Id"
        InsertCommand="INSERT INTO [News] ([Title], [News], [ImageUrl], [Date]) VALUES (@Title, @News, @ImageUrl, @Date)"
        SelectCommand="SELECT * FROM [News]"
        UpdateCommand="UPDATE [News] SET [Title] = @Title, [News] = @News, [ImageUrl] = @ImageUrl, [Date] = @Date WHERE [Id] = @Id">
        <DeleteParameters>
            <asp:Parameter Name="Id" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="Title" Type="String" />
            <asp:Parameter Name="News" Type="String" />
            <asp:Parameter Name="ImageUrl" Type="String" />
            <asp:Parameter Name="Date" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="Title" Type="String" />
            <asp:Parameter Name="News" Type="String" />
            <asp:Parameter Name="ImageUrl" Type="String" />
            <asp:Parameter Name="Date" Type="String" />
            <asp:Parameter Name="Id" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>

代码隐藏:

  protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
{
FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("FileUpload1");
string virtualFolder = "~/NewsPics/";
string physicalFolder = Server.MapPath(virtualFolder);
string fileName = Guid.NewGuid().ToString();
string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension));
string ImageUrl = virtualFolder + fileName + extension;

SqlDataSource1.InsertParameters["ImageUrl"].DefaultValue = ImageUrl;

}

我试过这个但没有结果。该文件被转移到文件系统[“NewsPics”],但路径未保存在数据库中。

0 个答案:

没有答案