Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File Upload control HasFile always false, name is always Empty String, no update panel used

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();
}
like image 698
EvanGWatkins Avatar asked Nov 18 '11 14:11

EvanGWatkins


1 Answers

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";
}
like image 144
EvanGWatkins Avatar answered Sep 26 '22 23:09

EvanGWatkins