File Upload control HasFile always false, name is

2019-01-26 13:11发布

I have a Details View that has a file upload field in it. When I fill out the information and upload a file (I have tried multiple files ranging from 9k to 6.8MB) all of the information (text fields) submit fine, but the uploaded file is always returning a false when I check the HasFile and always returns String.Empty when I check the file name.

Am I doing something wrong? The details view is in a Panel and Not an Update Panel

    <asp:Panel ID="pnlUpdate" runat="server"
        Visible="false">
        <h4 runat="server" id="h2SubCaption">Person Details</h4>
        <asp:DetailsView ID="dvAssignment" 
            runat="server" 
            AutoGenerateRows="false" 
            Width="100%"
            SkinID="SampleDetailsView" 
            CssSelectorClass="PrettyDetailsView"
            DataKeyNames="guidMemberId"
            DefaultMode="Edit"
            OnItemUpdating="dvAssignment_ItemUpdating" 
            OnModeChanging="dvAssignment_ModeChanging"
            AutoGenerateEditButton="True" >  
<Fields>   
<asp:TemplateField HeaderText="Nomination Letter">
                        <EditItemTemplate>
                            <asp:FileUpload runat="server" ID="fileuploadNomination" />
                        </EditItemTemplate>
                    </asp:TemplateField> .....

Code Behind:

        FileUpload _nomination = (FileUpload)dv.FindControl("fileuploadNomination");
    byte[] nominationByte = null;
    if (_nomination.FileName != string.Empty)
        nominationByte = _nomination.FileBytes;
    //if(_nomination.HasFile)
    //nominationByte = _nomination.FileBytes;

EDIT I added a Page_Load call and it looks as if the page is posting back when I click the Auto Generated Update Button for the DetailsView. This postback is probably clearing out my FileUpload field. Any ideas on how to get around it?

Edit #2 I have now put an update panel around the DetailsView and set the postback trigger the DetailsView (see below) and it still is not working, it seems to be clearing the upload control prior to submitting.

<asp:UpdatePanel ID="updatePnl" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Panel ID="pnlUpdate" runat="server"
            Visible="false">
            <h4 runat="server" id="h2SubCaption">Person Details</h4>
            <asp:DetailsView ID="dvAssignment" 
                runat="server" 
                AutoGenerateRows="false" 
                Width="100%"
                SkinID="SampleDetailsView" 
                CssSelectorClass="PrettyDetailsView"
                DataKeyNames="guidMemberId"
                DefaultMode="Edit"
                OnItemUpdating="dvAssignment_ItemUpdating" 
                OnModeChanging="dvAssignment_ModeChanging"
                AutoGenerateEditButton="True" >
                <FieldHeaderStyle Font-Bold="True" Width="150px" />
                <Fields>

                            <asp:FileUpload runat="server" ID="fileuploadNomination" />
                        </EditItemTemplate>
                    </asp:TemplateField>
 </Fields>
                </asp:DetailsView >       
            </asp:Panel>
         </ContentTemplate>
        <Triggers>
            <asp:PostBackTrigger ControlID="dvAssignment" /> 
        </Triggers>
    </asp:UpdatePanel>

Gridview Code as requested

 <asp:GridView ID="gvQuality" 
        runat="server" 
        AutoGenerateColumns="False"
        Width="100%"
        DataKeyNames="guidMemberId"
        CssSelectorClass="PrettyGridView"
        SkinID="SampleGridView"
        OnSelectedIndexChanged="gvQuality_SelectedIndexChanged" 
        onrowdatabound="gvQuality_RowDataBound">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                   <asp:LinkButton ID="btnViewDetails" runat="server" Text="Edit" CommandName="Select" />
                                    </ItemTemplate>
            </asp:TemplateField>

a few bound fields are after this (first name, last name, etc)

protected void gvQuality_SelectedIndexChanged(object sender, EventArgs e)
{
    Guid guidMemberId = (Guid)gvQuality.SelectedDataKey.Values["guidMemberId"];
    PortalDataContext db = new PortalDataContext(AuthenticatedUser.ConnectionString);
    h2SubCaption.InnerText = "Update Person";
    dvAssignment.ChangeMode(DetailsViewMode.Edit);

    dvAssignment.DataSource = LINQ Query Here
    dvAssignment.DataBind();
}

3条回答
Explosion°爆炸
2楼-- · 2019-01-26 13:32

I'm sorry if this is a dumb answer but I usually use on gridviews the selectedindexchanging to capture current row values. Have you tried that instead of the selectedindexchanged?

查看更多
祖国的老花朵
3楼-- · 2019-01-26 13:33

you can try Request.Files[0] get the upload file

查看更多
萌系小妹纸
4楼-- · 2019-01-26 13:36

Everyone, Thanks for all of the help but I figured it out. I had to set the Page.Form.Enctype = "multipart/form-data.

Here is the code for it to work!

protected void Page_Load(object sender, EventArgs e)
{
    this.Page.Form.Enctype = "multipart/form-data";
}
查看更多
登录 后发表回答