大家好我所有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 :(得分:0)
一种解决方案是让链接按钮执行正常的回发。一种方法是将项目模板中的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。
答案 1 :(得分:0)
在UpdatePanel中添加按钮PostBackTrigger
:
<asp:UpdatePanel ID="Panel1" runat="server">
<Triggers>
<asp:PostBackTrigger ControlID="DownloadButton" />
</Triggers>
<ContentTemplate>
<!-- GridView -->
</ContentTemplate>
</asp:UpdatePanel>
答案 2 :(得分:0)
将以下代码放在GridviewRowDatabound ..
中ScriptManager scriptMan = ScriptManager.GetCurrent(this);
LinkButton linkDow= e.Row.FindControl("linkDow") as LinkButton;
if (btn != null)
{
scriptMan.RegisterPostBackControl(linkDow);
}
了解更多详情Click here