我必须使用与网格视图绑定的链接按钮下载pdf文件。我已经以varbinary格式将pdf上传到数据库表中。下面给出的表cad_file列包含pdf文件。 点击" version_no"下拉列表,我必须使用gridview绑定pdf和所有其他数据。点击我想下载pdf。我怎样才能做到这一点。帮我找一个合适的解决方案。谢谢。
代码:
protected void ddlVersionNo_SelectedIndexChanged(object sender, EventArgs e)
{
ShadingAnalysisDataSetTableAdapters.tbl_CadFileUploadTableAdapter cd;
cd = new ShadingAnalysisDataSetTableAdapters.tbl_CadFileUploadTableAdapter();
DataTable dt = new DataTable();
dt = cd.GetGvCad2(ddlSiteID.SelectedValue, int.Parse(ddlVersionNo.SelectedValue));
gvCadPdf.DataSource = dt;
gvCadPdf.DataBind();
}
SQL:
SELECT Id, District, SiteName, Site_ID, Created_Date, Created_By, State, Updated_Date, Updated_By, Version_Status, Version_No, Cad_File, Cad_File_Name FROM tbl_CadFileUpload WHERE (Site_ID = @Site_ID) AND (Version_No = @Version_No)
ASPX:
DB:
ScreenShot1:
ScreenShot2:
答案 0 :(得分:0)
下面给出了使用链接按钮下载pdf的工作代码。谢谢大家的宝贵意见。
代码:
protected void lnkDownload_Click(object sender, EventArgs e)
{
LinkButton lnkbtn = sender as LinkButton;
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
int fileid = Convert.ToInt32(gvCadPdf.DataKeys[gvrow.RowIndex].Value.ToString());
string name, type;
using (SqlConnection con = new SqlConnection(strCon))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = " SELECT Id, Cad_File, Cad_File_Name, type From tbl_CadFileUpload WHERE Id=@Id";
cmd.Parameters.AddWithValue("@id", fileid);
cmd.Connection = con;
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
Response.ContentType = dr["type"].ToString();
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + dr["Cad_File_Name"] + "\"");
Response.BinaryWrite((byte[])dr["Cad_File"]);
Response.End();
}
}
}
}
ASPX:
<asp:GridView ID="gvCadPdf" runat="server" OnSelectedIndexChanged="gvCadPdf_SelectedIndexChanged" AutoGenerateColumns="False" DataKeyNames="Id">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Sl No" ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="State" HeaderText="State" ReadOnly="True" SortExpression="State" />
<asp:BoundField DataField="District" HeaderText="District" ReadOnly="True" SortExpression="District" />
<asp:BoundField DataField="SiteName" HeaderText="Site Name" ReadOnly="True" SortExpression="SiteName" />
<asp:BoundField DataField="Site_ID" HeaderText="Site ID" ReadOnly="True" SortExpression="Site_ID" />
<asp:BoundField DataField="Created_Date" HeaderText="Created Date" ReadOnly="True" SortExpression="Created_Date" />
<asp:BoundField DataField="Updated_Date" HeaderText="Updated Date" ReadOnly="True" SortExpression="Updated_Date" />
<asp:BoundField DataField="Created_By" HeaderText="Created By" ReadOnly="True" SortExpression="Created_By" />
<asp:BoundField DataField="Updated_By" HeaderText="Updated By" ReadOnly="True" SortExpression="Updated_By" />
<asp:BoundField DataField="Version_No" HeaderText="Version No" ReadOnly="True" SortExpression="Version_No" />
<asp:BoundField DataField="Cad_File_Name" HeaderText="Cad File Name" ReadOnly="True" SortExpression="Cad_File_Name" />
<asp:TemplateField HeaderText="FilePath">
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="lnkDownload_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
DB:
答案 1 :(得分:-1)
试试这段代码。
DropDown SelectedIndexChanged
protected void ddlVersionNo_SelectedIndexChanged(object sender, EventArgs e)
{
int Demo=ddlVersionNo.SelectedValue;
}
GridViewRowCommand
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "LinkButton")
{
ShadingAnalysisDataSetTableAdapters.tbl_CadFileUploadTableAdapter cd;
cd = new ShadingAnalysisDataSetTableAdapters.tbl_CadFileUploadTableAdapter();
DataTable dt = new DataTable();
dt = cd.GetGvCad2(ddlSiteID.SelectedValue, int.Parse(Demo));
gvCadPdf.DataSource = dt;
gvCadPdf.DataBind();
}
}