文件上传不起作用

时间:2013-09-24 21:04:05

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

我有以下标记:

 <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>

该页面基本上是几个设置,当按下“保存”按钮时,这些设置都会被提交。这是一个asp:Button,会导致Page_Load发生,从而清除FileUpload控件,导致HasFile属性始终为falseFileUpload

我知道FileUpload控件很乱,我应该使用别的东西,但时间限制阻止我从头开始做任何事情。奇怪的是,与此相同的代码在单独的页面上工作正常(在该页面上,Panel不在FileUpload中。)

如何让Save_OnClick保持过去的页面加载,或者更好的是,如何在不导致不必要的页面加载的情况下执行{{1}}代码隐藏?

感谢。

1 个答案:

答案 0 :(得分:1)

FileUpload需要完整的回发才能发挥作用。

在点击Save按钮之前,页面上的某些内容会产生回发,或者Save按钮位于UpdatePanel内并且未添加到Triggers }作为PostBackTrigger

<asp:PostBackTrigger ControlID="btnSave" />

我建议做以下事情以完全避免这种情况:

  1. 创建一个加载按钮(确保它存在于触发器中)
  2. 添加包含文件名的标签,以便用户知道文件已加载
  3. 点击bntLoadFile后,更新标签并保留文件或保存期间的任何内容。
  4. <强> 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>
    

    代码背后:

    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.
    }