kmarks2
kmarks2

Reputation: 4875

File upload does not ever work

I have the following markup:

 <asp:Panel runat="server" ID="pnlCallQueue" Visible="false">
    <tr>
       <td>&nbsp;</td>
       <td class="textBlue"><asp:Label runat="server" ID="lblQueueOnHoldFile" /></td>
        <td>
           <asp:CheckBox runat="server" Text="  On Hold Music &nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;  " ID="cbQueueOnHoldMusic" />
           File: <asp:FileUpload ID="fileOnHoldMusic" runat="server" />          
           <asp:Label runat="server" ID="lblUploadError" style="color: Red;" Visible="false" />
        </td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td class="textBlue"><label>Call Distribution: </label></td>
        <td>
           <asp:DropDownList runat="server" ID="ddlCallDistribution" style="width: 250px;">
             <asp:ListItem Value="ringall" Text="Ring All" Selected="True"></asp:ListItem>
             <asp:ListItem Value="leastrecent" Text="Least Recent" Selected="False"></asp:ListItem>
             <asp:ListItem Value="fewestcalls" Text="Fewest Calls" Selected="False"></asp:ListItem>
             <asp:ListItem Value="random" Text="Random" Selected="False"></asp:ListItem>
             <asp:ListItem Value="rrmemory" Text="Round Robin" Selected="False"></asp:ListItem>
           </asp:DropDownList>
        </td>
     </tr>
     <tr>
        <td>&nbsp;</td>
        <td class="textBlue"><label>Members: </label></td>
        <td>
           <asp:DropDownList runat="server" ID="ddlMembers" style="width: 200px;" />
           <asp:DropDownList runat="server" ID="ddlPriority" style="width: 46px;">
             <asp:ListItem>1</asp:ListItem>
             <asp:ListItem>2</asp:ListItem>
             <asp:ListItem>3</asp:ListItem>
             <asp:ListItem>4</asp:ListItem>
             <asp:ListItem>5</asp:ListItem>
             <asp:ListItem>6</asp:ListItem>
             <asp:ListItem>7</asp:ListItem>
             <asp:ListItem>8</asp:ListItem>
             <asp:ListItem>9</asp:ListItem>
             <asp:ListItem>10</asp:ListItem>
           </asp:DropDownList>
          <asp:Button runat="server" class="button" Text="Add Member" ID="btnAddMember" OnClick="btnAddMember_OnClick" />
        </td>
    </tr> 
    <tr>
      <asp:UpdatePanel ID="updQueueGrid" runat="server">
         <Triggers>
          <asp:PostBackTrigger ControlID="dgQueueMembers" />
          <asp:PostBackTrigger ControlID="btnAddMember" />
         </Triggers>
         <ContentTemplate>
         <td>
         </td>
         <td style="width: 100%; padding: 0pt;" colspan="2" align="left">
           <asp:DataGrid ID="dgQueueMembers" runat="server"  CssClass="mGrid" Visible="true"
                         AllowSorting="false" AllowPaging="false" AutoGenerateColumns="false" DataKeyField="id" Width="75%"
                         HeaderStyle-CssClass="GridHeader" ItemStyle-CssClass="GridItem" AlternatingItemStyle-CssClass="GridAltItem"
                         >
             <Columns>
               <asp:BoundColumn DataField="id" HeaderText="" Visible="false" >
                   <ItemStyle HorizontalAlign="center"  />
                   <HeaderStyle HorizontalAlign="center" />
               </asp:BoundColumn>
               <asp:BoundColumn DataField="agentExtension" HeaderText="Agent Extension">
                   <ItemStyle HorizontalAlign="center"  />
                   <HeaderStyle HorizontalAlign="center" />
               </asp:BoundColumn>
               <asp:BoundColumn DataField="agentPriority" HeaderText="Priority">
                   <ItemStyle HorizontalAlign="center" />
                   <HeaderStyle HorizontalAlign="center" />
               </asp:BoundColumn>
               <asp:TemplateColumn HeaderText="">
                   <itemstyle HorizontalAlign="center" />                            
                   <itemtemplate>
                       <asp:LinkButton runat="server" ID="lnkDelQueueMember" OnCommand="DeleteQueueMember" Text="Delete" CommandArgument=<%# Eval("id") %>></asp:LinkButton>
                   </itemtemplate>
               </asp:TemplateColumn>            
             </Columns>              
               <HeaderStyle BackColor="#eeeeee" />
           </asp:DataGrid>          
        </td>
       </ContentTemplate>
       </asp:UpdatePanel>
    </tr>
 </asp:Panel>

        ...

</asp:Content>

The page is basically several settings, which are all submitted when a Save button is pressed. This is an asp:Button, and causes Page_Load to occur, which wipes out the FileUpload control, causing it to always have a HasFile property to be false.

I know FileUpload controls are messy and that I should probably be using something else, but time contraints prevent me from starting anything from scratch. Oddly enough, code identical to this works fine on a separate page (on that page the FileUpload is not in a Panel, though).

How can I get this FileUpload to persist past page loads, or better still, how can I get my Save_OnClick codebehind to execute without causing the unnecessary page load?

Thanks.

Upvotes: 1

Views: 226

Answers (1)

Khan
Khan

Reputation: 18142

FileUploads require a full postback in order to work.

Either something on your page is incurring a postback before the Save button is clicked, or the Save button is inside of an UpdatePanel and is not added to the Triggers as a PostBackTrigger.

<asp:PostBackTrigger ControlID="btnSave" />

I would suggest doing the following to avoid the situation entirely:

  1. Create a Load button (Ensure this exists in the triggers)
  2. Add a Label which contains the filename, so the user is aware the file is loaded
  3. Upon clicking of bntLoadFile, update the label and persist the file or whatever you'd do during the save.

XHTML:

<asp:ScriptManager runat="server" ID="ScriptManager1" />
<asp:UpdatePanel runat="server">
<Triggers>
    <asp:PostBackTrigger ControlID="btnLoadFile" />
</Triggers>
    <ContentTemplate>
        <asp:FileUpload runat="server" ID="fileOnHoldMusic" />
        <asp:Button runat="server" ID="btnLoadFile" Text="Load File" onclick="btnLoadFile_Click" /><br />            
        <asp:Label runat="server" ID="lblFileTitle" Text="File: " /><asp:Label runat="server" ID="lblFile" />
    </ContentTemplate>
</asp:UpdatePanel>

Code behind:

protected void btnLoadFile_Click(object sender, EventArgs e)
{
    lblFile.Text = fileOnHoldMusic.FileName;

    // Do whatever you need to do with 
    // the file now.  Maybe persist data
    // for the Save event to use.
}

Upvotes: 1

Related Questions