【问题标题】:File upload does not ever work文件上传永远不起作用
【发布时间】:2013-09-30 06:47:39
【问题描述】:

我有以下标记:

 <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 属性为false

我知道FileUpload 控件很乱,我可能应该使用其他东西,但是时间限制使我无法从头开始任何事情。奇怪的是,与此相同的代码在单独的页面上运行良好(但在该页面上,FileUpload 不在Panel 中)。

我怎样才能让这个FileUpload 保持过去的页面加载,或者更好的是,我怎样才能让我的Save_OnClick 代码隐藏在不导致不必要的页面加载的情况下执行?

谢谢。

【问题讨论】:

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


    【解决方案1】:

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

    在单击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.
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-27
      • 2016-09-13
      • 2014-01-09
      • 2014-04-27
      • 1970-01-01
      • 1970-01-01
      • 2021-04-10
      • 1970-01-01
      相关资源
      最近更新 更多