2011-10-11 82 views
0

大家好我有一個grid view其中我使用AJAX update panel。我將從Database的項目綁定到網格視圖,以便當用戶點擊Download這是可用我想要download該文件並提示用戶保存或取消。我寫的所有,當我用了更新面板,我能夠提示該文件,但與更新面板相同的代碼沒有給我我所需要的輸出運行它相關的代碼..無法輸出文件,當我點擊下載網格視圖

我的設計

<asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
<ContentTemplate> 
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Font-Names="Arial" Font-Size="11pt" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" OnRowCommand="GridView1_RowCommand"> 
<Columns> 
<asp:TemplateField> 
<HeaderTemplate> 
<asp:CheckBox ID="CheckAll" onclick="return check_uncheck (this);" runat="server" /> 
</HeaderTemplate> 
    <ItemTemplate> 
<asp:Label ID="ID" Visible="false" Text='<%# DataBinder.Eval (Container.DataItem, "Id") %>' runat="server" /> 
    <asp:CheckBox ID="deleteRec" onclick="return check_uncheck (this);" runat="server" /> 
    </ItemTemplate> 
</asp:TemplateField> 
<asp:TemplateField HeaderText="Id" SortExpression="Id"> 
<EditItemTemplate> 
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Id") %>'></asp:Label> 
</EditItemTemplate> 
<ItemTemplate> 
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Id") %>'></asp:Label> 
</ItemTemplate> 
</asp:TemplateField> 
<asp:TemplateField HeaderText="FileName" SortExpression="FileName"> 
<EditItemTemplate> 
<asp:Label ID="LblFileName" runat="server" Text='<%# Eval("File_Name") %>'>  </asp:Label> 
</EditItemTemplate> 
<ItemTemplate> 
<asp:Label ID="LblFileName1" runat="server" Text='<%# Bind("File_Name") %>'></asp:Label> 
    <asp:ImageButton ID="img" runat="Server" CommandName="Image" ImageUrl="~/images/view1.png" /> 
    </ItemTemplate> 
    </asp:TemplateField> 
    <asp:TemplateField HeaderText="DownLoad" SortExpression="FileName"> 
    <ItemTemplate> 
<asp:LinkButton ID="linkDow" runat="server" CommandName="DOWNLOAD">Download</asp:LinkButton> 
    </ItemTemplate> 
    </asp:TemplateField> 
    </Columns> 
    <AlternatingRowStyle BackColor="White" /> 
    <RowStyle BackColor="#F7F7DE" /> 
    <FooterStyle BackColor="#CCCC99" /> 
    <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" /> 
    <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" /> 
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" /> 
</asp:GridView> 
<asp:Panel ID="pnlAddEdit" runat="server" CssClass="modalPopup" Style="display: none; 
       height: auto; width: auto;"> 
    <br /> 
    <table align="center"> 
    <tr> 
    <td> 
    <asp:GridView ID="dynamicGridView" runat="server" AutoGenerateColumns="True" Font-Names="Arial" Font-Size="11pt" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"> 
    <AlternatingRowStyle BackColor="White" /> 
    <RowStyle BackColor="#F7F7DE" /> 
    <FooterStyle BackColor="#CCCC99" /> 
    <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" /> 
    <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" /> 
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" /> 
    </asp:GridView> 
</td> 
</tr> 
<tr> 
    <td align="center"> 
    <asp:Button ID="btnCancel" runat="server" Text="Ok" OnClientClick="return Hidepopup()" /> 
    </td> 
    </tr> 
    </table> 
    </asp:Panel> 
    <asp:LinkButton ID="lnkFake" runat="server"></asp:LinkButton> 
<cc1:ModalPopupExtender ID="popup" runat="server" DropShadow="false" PopupControlID="pnlAddEdit" TargetControlID="lnkFake" BackgroundCssClass="modalBackground"> 
</cc1:ModalPopupExtender> 
</ContentTemplate> 
<Triggers> 
<asp:AsyncPostBackTrigger ControlID="GridView1" /> 
</Triggers> 
</asp:UpdatePanel> 

這是我在Row command事件

if (e.CommandName == "DOWNLOAD") 
    { 
     StringBuilder sb = new StringBuilder(); 
     String strConnString = System.Configuration.ConfigurationSettings.AppSettings.Get("ConnectionString"); 
     string FileName = string.Empty; 
     FileName = "venkat.txt"; 
     string strPath1 = Server.MapPath("AchTemplates") + "\\" + "venkat.txt"; 

     int id1 = 0; 

     //string strLine = string.Empty; 
     // string[] lines = null; 

     MySqlConnection cnn = new MySqlConnection(strConnString); 
     cnn.Open(); 
     GridViewRow row = (GridViewRow)((Control)e.CommandSource).Parent.Parent; 
     //Label l = (Label)grdACH.Rows[row.RowIndex].Cells[1].FindControl("Label1"); 
     //Label l = (Label)grdACH.Rows[row.RowIndex].Cells[1].FindControl("lblid"); 
     //strid = ((Label)grdACH.Rows[row.RowIndex].FindControl("lblid")).Text.ToString(); 
     //id = Convert.ToInt16(l.Text); 
     string strid = ((Label)GridView1.Rows[row.RowIndex].FindControl("Label1")).Text.ToString(); 
     id1 = Convert.ToInt16(strid); 
     string selectSQL = "Select File_Data from tblachmaster where ID IN (" + id1 + ")"; 

     MySqlCommand cmd2 = new MySqlCommand(selectSQL, cnn); 
     // cmd1.Parameters.Add("@_id", SqlDbType.Int).Value = id; 
     //DataTable dt1 = GetData1(cmd1); 
     DataSet oDataSet = new DataSet(); 
     MySqlDataAdapter oAdapter = new MySqlDataAdapter(); 
     // oCmd.Parameters.AddWithValue("_FedTaxID", FedTaxID); 
     oAdapter.SelectCommand = cmd2; 

     oAdapter.Fill(oDataSet); 

     DataTable dt1 = oDataSet.Tables[0]; 
     //string[] readText = File.ReadAllLines(strPath); 
     if (dt1 != null) 
     { 
      for (int i = 0; i < dt1.Rows.Count; i++) 
      { 
       Byte[] bytes = (Byte[])dt1.Rows[i]["File_Data"]; 
       string text = Encoding.UTF8.GetString(bytes); 
       //lines = Regex.Split(text, "\r\n"); 

       // sb.Append(lines.ToString()); 

       sb.Append(text.ToString()); 

       // strLine = convertArrayToString(lines); 
      } 
     } 
     File.WriteAllText(strPath1, sb.ToString()); 
     Response.Clear(); 
     Response.Buffer = true; 
     Response.AddHeader("content-disposition", "attachment;filename=genACH(CCD).txt"); 
     Response.Charset = ""; 
     Response.ContentType = "application/text"; 
     Response.Output.Write(strPath1); 
     Response.Flush(); 
     Response.End(); 

    } 

代碼,但我不能解僱一個我需要的任何一個可以幫助我

+0

您將無法使用UpdatePanel與下載請求一起使用,如果您通過回發到服務器來下載文件。 UpdatePanel會搞砸了...... –

+0

那麼我怎麼能達到這個按我的要求 – Dotnet

+0

刪除UpdatePanel,或添加觸發器只回應某些事件 –

回答

0

一種解決方案是具有L-墨水按鈕執行正常回發。一種方法是在您的項目模板改變linkbutton來是這樣的:

<a target="_blank" href='FileDownloader.aspx?Id='<%# Eval("Id") %>'>Download</a> 

,然後創建一個名爲FileDownloader.aspx頁的東西,如:

protected void Page_Load(object sender, EventArgs e) 
{ 

     StringBuilder sb = new StringBuilder(); 
     String strConnString = System.Configuration.ConfigurationSettings.AppSettings.Get("ConnectionString"); 
     string FileName = string.Empty; 
     FileName = "venkat.txt"; 
     string strPath1 = Server.MapPath("AchTemplates") + "\\" + "venkat.txt"; 

     int id1 = 0; 

     MySqlConnection cnn = new MySqlConnection(strConnString); 
     cnn.Open(); 
     GridViewRow row = (GridViewRow)((Control)e.CommandSource).Parent.Parent; 


     id1 = Convert.ToInt16(Request.QueryString["Id"]); 
     string selectSQL = "Select File_Data from tblachmaster where ID IN (" + id1 + ")"; 

     MySqlCommand cmd2 = new MySqlCommand(selectSQL, cnn); 
     DataSet oDataSet = new DataSet(); 
     MySqlDataAdapter oAdapter = new MySqlDataAdapter(); 
     oAdapter.SelectCommand = cmd2; 

     oAdapter.Fill(oDataSet); 

     DataTable dt1 = oDataSet.Tables[0]; 
     if (dt1 != null) 
     { 
      for (int i = 0; i < dt1.Rows.Count; i++) 
      { 
       Byte[] bytes = (Byte[])dt1.Rows[i]["File_Data"]; 
       string text = Encoding.UTF8.GetString(bytes); 

       sb.Append(text.ToString()); 

      } 
     } 
     File.WriteAllText(strPath1, sb.ToString()); 
     Response.Clear(); 
     Response.Buffer = true; 
     Response.AddHeader("content-disposition", "attachment;filename=genACH(CCD).txt"); 
     Response.Charset = ""; 
     Response.ContentType = "application/text"; 
     Response.Output.Write(strPath1); 
     Response.Flush(); 
     Response.End(); 

} 

注意:上面的代碼完全未經測試。我拿了你的代碼,並粘貼它只刪除你的意見,並作出一點調整,以獲得Id。

0

添加按鈕,在UpdatePanel的一個PostBackTrigger:內GridviewRowDatabound

<asp:UpdatePanel ID="Panel1" runat="server"> 
    <Triggers> 
     <asp:PostBackTrigger ControlID="DownloadButton" /> 
    </Triggers> 
    <ContentTemplate> 
     <!-- GridView --> 
    </ContentTemplate> 
</asp:UpdatePanel> 
0

認沽以下代碼..

ScriptManager scriptMan = ScriptManager.GetCurrent(this); 
LinkButton linkDow= e.Row.FindControl("linkDow") as LinkButton; 
if (btn != null) 
{ 
scriptMan.RegisterPostBackControl(linkDow); 
} 

更多細節Click here