2016-05-19 22 views
0

ファイルをアップロードするときにGridViewに投稿されたファイル名を取得できません。 ここで私が試したことです。ファイルをアップロードするときに、GridViewに投稿されたファイル名を取得できません。

ASPXファイルの内容は以下の通りです:

protected void GVProtocol_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
     try 
     { 
      int Index = Convert.ToInt32(e.CommandArgument); 
      if (e.CommandName == "upload") 
      { 
       string strFilePathTemp = Server.MapPath("~/ProtocolCommunication/"); 

       //int Index = ((GridViewRow)((sender as Control)).NamingContainer).RowIndex; 



       LinkButton lbDownload = (LinkButton)GVProtocol.Rows[Index].FindControl("btnDownload"); 
       lbDownload.Text = "<span class='glyphicon glyphicon-download-alt'>"; 
       Label lblFilePathTemp = (Label)GVProtocol.Rows[Index].FindControl("lblFilePath"); 
       lblFilePathTemp.Text = strFilePathTemp; 
       FileUpload fileUpload = (FileUpload)GVProtocol.Rows[Index].FindControl("FileUpload"); 

       if(fileUpload != null) 
       { 
        string fileName = Path.GetFileName(fileUpload.PostedFile.FileName); 
        fileUpload.PostedFile.SaveAs(strFilePathTemp + fileName); 
       } 


      } 
      if (e.CommandName == "download") 
      { 
       Label lblFilePathTemp = (Label)GVProtocol.Rows[Index].FindControl("lblFilePath"); 
       string fileNameAndPath = lblFilePathTemp.Text.ToString(); 


       string file = Path.GetFileName(fileNameAndPath); 
       Response.ContentType = ContentType; 
       Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(fileNameAndPath)); 
       Response.TransmitFile(Server.MapPath("~/ProtocolCommunication/"));      
       Response.End(); 
      } 
     } 
     catch(Exception ex) 
     { 
      throw ex; 
     }    
    } 

上記の問題のための可能な解決策を探して:

<asp:GridView ID="GVProtocol" runat="server" AllowPaging="True" 
           HorizontalAlign="Center" CssClass="DGList" HeaderStyle-CssClass="DGHeader" 
           RowStyle-CssClass="DGRow" AlternatingRowStyle-CssClass="DGRowAlt" OnRowCommand="GVProtocol_RowCommand" 
           OnPageIndexChanging="GVProtocol_PageIndexChanging" Height="90px" Width="100%" AutoGenerateColumns="False"> 
           <Columns> 
            <asp:TemplateField HeaderText="SL NO" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="20"> 
             <ItemTemplate> 
              <%# (((GridView) ((GridViewRow) Container).Parent.Parent).PageSize + Container.DataItemIndex + 1)- 15 %> 
             </ItemTemplate> 
             <HeaderStyle HorizontalAlign="Center"></HeaderStyle> 
             <ItemStyle HorizontalAlign="Center"></ItemStyle> 
            </asp:TemplateField>                              
            <asp:BoundField HeaderText="Name" DataField="NAME" /> 
            <asp:BoundField HeaderText="Unit" DataField="UNIT" /> 
            <asp:BoundField HeaderText="Standard Value" DataField="STANDARD_VALUE" /> 
            <asp:TemplateField HeaderText="Upload XML" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center"> 
             <ItemTemplate> 
              <asp:FileUpload ID="FileUpload" runat="server" EnableViewState="true" /> 
              <asp:Button runat="server" ID="btnUpload" Text="upload" CommandArgument="<%# Container.DataItemIndex %>" CommandName="upload" /> 
             </ItemTemplate> 
            </asp:TemplateField> 
            <asp:TemplateField HeaderText="FilePath" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="200"> 
             <ItemTemplate> 
              <asp:Label runat="server" ID="lblFilePath">-</asp:Label>             
              <asp:LinkButton ID="btnDownload" runat="server" CommandName="download" CommandArgument="<%# Container.DataItemIndex %>"></asp:LinkButton> 
             </ItemTemplate>                                 
            </asp:TemplateField> 
           </Columns> 

           <PagerStyle CssClass="pagerStyle" /> 
           <HeaderStyle Wrap="False" CssClass="DGHeader" /> 
           <RowStyle CssClass="DGRow" /> 
           <AlternatingRowStyle CssClass="DGRowAlt" HorizontalAlign="Center" /> 
          </asp:GridView> 

これは、ASPXのコンテンツの上のための分離コードファイルです。

+0

FileUploadコントロールを取得していますか? – KanisXXX

+0

はい、しかし投稿されたファイルを取得していない、ヌル値を取得しています –

+1

こんにちは,,私は解決策を得ました、動的に作成されたコントロールIDを保持するためにpostbacktriggerと ''を使用します。これを使用すると、任意のgridview行にファイルをアップロードしてダウンロードできます。 –

答えて

0

私は解決策を得ました。 asp:updatePanelコントロールとpostbacktriggerを使用して、girdviewで動的に作成されたコントロールIDを保持しました。これを使用して、ファイルをgridview行のいずれかにアップロードしてダウンロードすることができます。

関連する問題