文件上传永远不起作用

本文关键字:不起作用 永远 文件 | 更新日期: 2023-09-27 18:32:50

我有以下标记:

 <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控件,使其始终具有要falseHasFile属性。

我知道FileUpload控件很混乱,我可能应该使用其他东西,但时间限制使我无法从头开始。 奇怪的是,与此相同的代码在单独的页面上工作正常(尽管在该页面上,FileUpload不在Panel中)。

如何让此FileUpload保留过去的页面加载,或者更好的是,如何使我的Save_OnClick代码隐藏执行而不导致不必要的页面加载?

谢谢。

文件上传永远不起作用

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

在单击Save按钮之前,页面上的某些内容导致回发,或者Save按钮位于UpdatePanel内,并且未作为PostBackTrigger添加到Triggers中。

<asp:PostBackTrigger ControlID="btnSave" />

我建议执行以下操作以完全避免这种情况:

  1. 创建加载按钮(确保触发器中存在此按钮)
  2. 添加包含文件名的标签,以便用户知道文件已加载
  3. 单击 bntLoadFile 后,更新标签并保留文件或您在保存期间执行的任何操作。

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