开发者

FileUpload inside a DetailsView: HasFile is always false

开发者 https://www.devze.com 2023-02-27 09:53 出处:网络
First and foremost, I am NOT using an UpdatePanel -- it looks like that\'s a common issue, but I didn\'t even know what one was before I googled this issue.

First and foremost, I am NOT using an UpdatePanel -- it looks like that's a common issue, but I didn't even know what one was before I googled this issue.

I have a DetailsView attempting to upload a file and insert the filename into the database. Problem is, HasFile is always turning up false! Any idea what I've done wrong?

C#:

public partial class DocManager : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!(System.Convert.ToBoolean(Session["Admin"])))
            Response.Redirect("Index.aspx");
    }

protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
    FileUpload fu1 = (FileUpload) DetailsView1.FindControl("FileUpload1");
    if (fu1 == null)
    {
        e.Cancel = true;
        StatusLabel.Text = "Could not find file upload";
    }
    if (fu1.HasFile)
    {
        try
        {
            string filename = Path.GetFileName(fu1.FileName);
            fu1.SaveAs(Server.MapPath("~/Docs/") + filename);
            StatusLabel.Text = "Upload status: File uploaded!";
            e.Values["FileName"] = filename;
        }
        catch (Exception ex)
        {
            StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
        }

    }
    else
    {
        e.Cancel = true;
        StatusLabel.Text = "No file uploaded";
        return;
    }

    DropDownList dd1 = (DropDownList)DetailsView1.FindControl("DropDownList2");
    DropDownList dd2 = (DropDownList)DetailsView1.FindControl("DropDownList4");
    e.Values["Type"] = dd1.SelectedValue;
    e.Values["MeetingID"] = dd2.SelectedValue;

}

protected void DetailsView1_ItemEditing(object sender, DetailsViewInsertEventArgs e)
{
    FileUpload fu1 = (FileUpload)DetailsView1.FindControl("FileUpload2");
    if (fu1 == null)
        e.Cancel = true;
    if (fu1.HasFile) {
        try
        {
            string filename = Path.GetFileName(fu1.FileName);
            fu1.SaveAs(Server.MapPath("~/Docs/") + filename);
            StatusLabel.Text = "Upload status: File uploaded!";
            e.Values["FileName"] = filename;
        }
        catch (Exception ex)
        {
            StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
        }
    }
    else
        e.Cancel = true;

    DropDownList dd1 = (DropDownList)DetailsView1.FindControl("DropDownList1");
    DropDownList dd2 = (DropDownList)DetailsView1.FindControl("DropDownList3");
    e.Values["Type"] = dd1.SelectedValue;
    e.Values["MeetingID"] = dd2.SelectedValue;

}

}

The only thing in PageLoad is a check to be sure the user is logged in properly, nothing else should be running before that.

The form itself:

<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" 
    AutoGenerateRows="False" DataSourceID="Docs" DefaultMode="Insert"
     OnItemInserting="DetailsView1_ItemInserting"
     OnItemEditing="DetailsView1_ItemEditing"
    >
    <Fields>
        <asp:BoundField DataField="Documents.Title" HeaderText="Title" 
            SortExpression="Documents.Title" />
        <asp:TemplateField HeaderText="Type" SortExpression="Type">
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server">
                    <asp:ListItem Selected="True" Value="Presentation">Presentation</asp:ListItem>
                    <asp:ListItem Value="Handout">Handout</asp:ListItem>
                    <asp:ListItem Value="Minutes">Minutes</asp:ListItem>
                    <asp:ListItem Value="Agenda">Agenda</asp:ListItem>
                    <asp:ListItem Value="Other">Other</asp:ListItem>
                </asp:DropDownList>
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:DropDownList ID="DropDownList2" runat="server">
                    <asp:ListItem Selected="True" Value="Presentation">Presentation</asp:ListItem>
                    <asp:ListItem Value="Handout">Handout</asp:ListItem>
                    <asp:ListItem Value="Minutes">Minutes</asp:ListItem>
                    <asp:ListItem Value="Agenda">Agenda</asp:ListItem>
                    <asp:ListItem Value="Other">Other</asp:ListItem>
                </asp:DropDownList>
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Bind("Type") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="MeetingID" SortExpression="MeetingID">
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="Meetings" 
                    DataTextField="Title" DataValueField="MeetingID">
                </asp:DropDownList>
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:DropDownList ID="DropDownList4" runat="server" DataSourceID="Meetings" 
                    DataTextField="Title" DataValueField="MeetingID">
                </asp:DropDownList>
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("MeetingID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="FileName" SortExpression="FileName">
            <EditItemTemplate>
                <asp:FileUpload ID="FileUpload2" runat="server" />
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:FileUpload ID="FileUpload1" runat="server" />
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("FileName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
    </Fields>
</asp:DetailsView>

ETA: The entire form:

<asp:Content ID="Content2" ContentPlaceHolderID="mainContent" Runat="Server">
<h1>Document Manager</h1>

<h2>Existing Documents</h2>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
    AutoGenerateColumns="False" DataSourceID="Docs"
    OnRowDeleting="GridView1_RowDeleting"
    DataKeyNames="DocID" BackColor="White" BorderColor="#E7E7FF" 
    BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal">
    <AlternatingRowStyle BackColor="#F7F7F7" />
    <Columns>
        <asp:BoundField DataField="Documents.Title" HeaderText="Title" 
            SortExpression="Documents.Title" />
        <asp:BoundField DataField="FileName" HeaderText="FileName" 
            SortExpression="FileName" />
        <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
        <asp:BoundField DataField="Meetings.Title" HeaderText="Meeting" 
            SortExpression="Meetings.Title" />
        <asp:CommandField ShowDeleteButton="True" ShowSelectButton="True" />
        <asp:HyperLinkField DataNavigateUrlFields="FileName" Text="Download" />
    </Columns>
    <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
    <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
    <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
    <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
    <SortedAscendingCellStyle BackColor="#F4F4FD" />
    <SortedAscendingHeaderStyle BackColor="#5A4C9D" />
    <SortedDescendingCellStyle BackColor="#D8D8F0" />
    <SortedDescendingHeaderStyle BackColor="#3E3277" />
</asp:GridView>
<h2>Add New</h2>
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" 
    AutoGenerateRows="False" DataSourceID="Docs" DefaultMode="Insert"
     OnItemInserting="DetailsView1_ItemInserting"
     OnItemEditing="DetailsView1_ItemEditing"
    >
    <Fields>
        <asp:BoundField DataField="Documents.Title" HeaderText="Title" 
            SortExpression="Documents.Title" />
        <asp:TemplateField HeaderText="Type" SortExpression="Type">
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server">
                    <asp:ListItem Selected="True" Value="Presentation">Presentation</asp:ListItem>
                    <asp:ListItem Value="Handout">Handout</asp:ListItem>
                    <asp:ListItem Value="Minutes">Minutes</asp:ListItem>
                    <asp:ListItem Value="Agenda">Agenda</asp:ListItem>
                    <asp:ListItem Value="Other">Other</asp:ListItem>
                </asp:DropDownList>
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:DropDownList ID="DropDownList2" runat="server">
                    <asp:ListItem Selected="True" Value="Presentation">Presentation</asp:ListItem>
                    <asp:ListItem Value="Handout">Handout</asp:ListItem>
                    <asp:ListItem Value="Minutes">Minutes</asp:ListItem>
                    <asp:ListItem Value="Agenda">Agenda</asp:ListItem>
                    <asp:ListItem Value="Other">Other</asp:ListItem>
                </asp:DropDownList>
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Bind("Type") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="MeetingID" SortExpression="MeetingID">
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="Meetings" 
                    DataTextField="Title" DataValueField="MeetingID">
                </asp:DropDownList>
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:DropDownList ID="DropDownList4" runat="server" DataSourceID="Meetings" 
                    DataTextField="Title" DataValueField="MeetingID">
                </asp:DropDownList>
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("MeetingID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="FileName" SortExpression="FileName">
            <EditItemTemplate>
                <asp:FileUpload ID="FileUpload2" runat="server" />
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:FileUpload ID="FileUpload1" runat="server" />
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("FileName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
    </Fields>
</asp:DetailsView>
<asp:AccessDataSource ID="Docs" runat="server" 
    DataFile="~/App_Data/Database1.accdb" 
    DeleteCommand="DELETE FROM [Documents] WHERE [DocID] = ?" 
    InsertCommand="INSERT INTO [Documents] ([Title], [Type], [FileName], [MeetingID]) VALUES (@Title, @Type, @FileName, @MeetingID)" 
    SelectCommand="SELECT Documents.DocID, Documents.Title, Documents.FileName, Documents.Type, Meetings.Title, Documents.MeetingID FROM 
    (Documents LEFT OUTER JOIN Meetings ON Documents.MeetingID = Meetings.MeetingID)" 
    UpdateCommand= "UPDATE [Documents] SET [Title] = ?, [Type] = ?, [FileName] = ?, [MeetingID] = ? WHERE [DocID] = ?">
    <DeleteParameters>
        <asp:Parameter Name="DocID" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="Documents.Title" Type="String" />
        <asp:Parameter Name="Type" Type="String" />
        <asp:Parameter Name="FileName" Type="String" />
        <asp:Parameter Name="MeetingID" Type="Int32" />
    </InsertParameters>
    <SelectParameters>
        <asp:SessionParameter Name="?" SessionField="UserID" />
    </SelectParameters>
    <UpdateParameters>
        &l开发者_开发问答t;asp:Parameter Name="Title" Type="String" />
        <asp:Parameter Name="Type" Type="String" />
        <asp:Parameter Name="FileName" Type="String" />
        <asp:Parameter Name="MeetingID" Type="Int32" />
        <asp:Parameter Name="DocID" Type="Int32" />
    </UpdateParameters>
</asp:AccessDataSource>
<asp:Label ID="StatusLabel" runat="server" Text="Label"></asp:Label>
<asp:AccessDataSource ID="Meetings" runat="server" 
    DataFile="~/App_Data/Database1.accdb" 
    SelectCommand="SELECT [MeetingID], [Title] FROM [Meetings] WHERE ([AdminID] = ?)">
    <SelectParameters>
        <asp:SessionParameter Name="AdminID" SessionField="UserID" Type="Int32" />
    </SelectParameters>
</asp:AccessDataSource>

I also edited in the rest of the c# code above, it wasn't as long

The form element in the master page:

<body style="background-color: rgb(231, 231, 255);height:100%;margin:0px;padding:0px">
<form id="form1" runat="server">

ETA: I found the problem. I'd been using an empty text file as a test - which was reading as 0 bytes, confusing the file uploader into thinking it had no file. Adding some text to the file makes it work perfectly.

Thanks for the help everyone!


I found the problem. I'd been using an empty text file as a test - which was reading as 0 bytes, confusing the file uploader into thinking it had no file. Adding some text to the file makes it work perfectly.

Ranting about why a file with a size of 0 bytes is not the same thing as no file at all shall be done elsewhere.

0

精彩评论

暂无评论...
验证码 换一张
取 消