2011-03-31 174 views
0

我有一個網格,文件名顯示爲鏈接按鈕。但是當我點擊鏈接按鈕時,文件無法打開。請參閱cs文件代碼方法來打開文件。文件沒有打開

protected void OpenFile(object sender, EventArgs e) 
    { 
     LinkButton btn = (LinkButton)sender; 
     string fileName = btn.Attributes["FileName"].ToString(); 

     string path = Server.MapPath(".") + "\\Files\\" + fileName; 
     if (File.Exists(path)) 
     { 
      Response.AppendHeader("content-disposition", "attachment; filename=" + fileName); 
      string type = "Application/word"; 
      if (type != "") 
       Response.ContentType = type; 
      Response.WriteFile(path); 
      Response.End(); 
     } 
     else 
     { 
     } 

     //System.Diagnostics.Process.Start(@path); 
     //Response.Write(path); 
    } 

這裏的GridView的代碼:

<asp:GridView ID="gdvMainList" runat="server" CssClass="Grid" 
               AutoGenerateColumns="False" DataSourceID="dtsFilesUploaded" 
               AllowPaging="True" DataKeyNames="Id, FileName" 
               onrowdatabound="gdvMainList_RowDataBound"> 
               <Columns> 
                <ucc:CommandFieldControl HeaderText="Actions" ShowDeleteButton="true" ButtonType="Image" 
                 DeleteImageUrl="~/App_Themes/Default/images/delete.png" ShowEditButton="true" 
                 EditImageUrl="~/App_Themes/Default/images/edit.png" ShowCancelButton="true" CancelImageUrl="~/App_Themes/Default/images/cancel.png" 
                 UpdateImageUrl="~/App_Themes/Default/images/update.png" DeleteConfirmationText="Are you sure you want to delete?"> 
                 <ItemStyle HorizontalAlign="Center" Width="60px" /> 
                 <HeaderStyle HorizontalAlign="Center" /> 
                </ucc:CommandFieldControl> 
                <asp:TemplateField HeaderText="File Name"> 
                 <ItemTemplate> 
                  <asp:LinkButton ID="lblFileName" runat="server" Text='<%# Bind("FileName") %>' OnClick="OpenFile" CausesValidation="false"></asp:LinkButton> 
                 </ItemTemplate> 
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="Uploaded On"> 
                 <ItemTemplate> 
                  <asp:Label ID="lblCreatedDate" runat="server" Text='<%# Bind("CreatedDateTime","{0:d}") %>'></asp:Label> 
                 </ItemTemplate> 
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="Category"> 
                 <ItemTemplate> 
                  <asp:Label ID="lblglCategoryId" runat="server" Text='<%# Bind("glCategoryId") %>'>' ></asp:Label> 
                 </ItemTemplate> 
                 <EditItemTemplate> 
                  <asp:DropDownList ID="ddlglCategoryId" runat="server" CssClass="textEntry2" DataSourceID="dtsglCategoryId" 
                   DataTextField="LookupItem" DataValueField="Id" AppendDataBoundItems="true"> 
                  </asp:DropDownList> 
                 </EditItemTemplate> 
                </asp:TemplateField> 

                <asp:BoundField DataField="Description" HeaderText="Description" ReadOnly="true" /> 
               </Columns> 
               <EmptyDataTemplate> 
                <div class="divEmptyListingGrid"> 
                 --- No Files Exists --- 
                </div> 
               </EmptyDataTemplate> 
              </asp:GridView> 

任何幫助將不勝感激。謝謝 !

+0

你可以試試「字符串路徑=」 ...行puttin一個斷點,看到該變量路徑獲取正確的文件路徑。 – 2011-03-31 07:09:34

+0

究竟是什麼問題?你是否遇到異常? OpenFile甚至被執行了嗎? – 2011-03-31 07:10:22

+0

@Elad Lachmi:路徑具有價值。我檢查了它。 @Daniel Hilgarth:沒有發生異常。但是我的aspx代碼在更新窗格中。這是問題嗎? – asma 2011-03-31 07:46:07

回答

1

響應上下文在更新面板中不可用。

另一種可以在這裏找到:http://forums.asp.net/p/1030846/1414305.aspx

+0

我能做些什麼?請幫助。問題 – asma 2011-03-31 08:30:39

+0

我編輯我的回答 – Anuraj 2011-03-31 08:54:29

+0

謝謝..但我有一個問題。我有一個鏈接按鈕,我想要的一樣。 我寫了我的aspx文件 但是,這給了我的更新面板中沒有這樣的控制的例外 請幫我關於這個。我能做什麼?????? – asma 2011-03-31 09:52:18