C# 从gridview访问数据以在asp.net中下载PDF文档
上面显示的是我的网格视图,包含教员的详细信息,下载链接表示要下载的教员的pdf格式C# 从gridview访问数据以在asp.net中下载PDF文档,c#,asp.net,sql-server-2008,pdf,C#,Asp.net,Sql Server 2008,Pdf,上面显示的是我的网格视图,包含教员的详细信息,下载链接表示要下载的教员的pdf格式 FID NAME M001 Faculty1 download M002 Faculty2 download M003 Faculty3 download 我的网格视图代码如下所示 va = (string)Session["FID"];
FID NAME
M001 Faculty1 download
M002 Faculty2 download
M003 Faculty3 download
我的网格视图代码如下所示
va = (string)Session["FID"];
protected void DownloadFile(object sender, EventArgs e)
{
DataRow dr = GetData("select * from Personal_det where FID ='"+va+"'").Rows[0];
Document doc = new Document(PageSize.A4, 88f, 88f, 10f, 10f);
Font NormalFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, BaseColor.BLACK);
using (System.IO.MemoryStream m = new System.IO.MemoryStream())
{
PdfWriter w = PdfWriter.GetInstance(doc, m);
Phrase phrase = null;
PdfPCell cell = null;
PdfPTable table = null;
BaseColor color = null;
Paragraph para = null;
Font times = null;
BaseFont bfTimes = null;
doc.Open();
table = new PdfPTable(2);
cell = PhraseCell(new Phrase("Faculty Profile", FontFactory.GetFont("Arial", 12, Font.UNDERLINE, BaseColor.BLACK)), PdfPCell.ALIGN_CENTER);
//table.SpacingBefore = 20f;
cell.Colspan = 2;
table.AddCell(cell);
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 30f;
//FID
table.AddCell(PhraseCell(new Phrase("Faculty Code:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
table.AddCell(PhraseCell(new Phrase("mahe" + dr["FID"].ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
//Name
table.AddCell(PhraseCell(new Phrase("Name:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
table.AddCell(PhraseCell(new Phrase(dr["Name"].ToString(), FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
//Date of Birth
table.AddCell(PhraseCell(new Phrase("Date of Birth:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
table.AddCell(PhraseCell(new Phrase(Convert.ToDateTime(dr["DOB"]).ToString("dd MMMM, yyyy"), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
//Phone Number
table.AddCell(PhraseCell(new Phrase("Phone Number:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
table.AddCell(PhraseCell(new Phrase(Convert.ToInt64(dr["MobileNo"]).ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
//Mail Id
table.AddCell(PhraseCell(new Phrase("Email ID:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
table.AddCell(PhraseCell(new Phrase(dr["EmailId"].ToString(), FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
//Address
table.AddCell(PhraseCell(new Phrase("Res Address:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
phrase = new Phrase(new Chunk(dr["add1"] + "\n " + dr["add2"] + "\n " + dr["add3"] + "\n " + dr["Pincode"], FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
table.SpacingAfter = 30f;
doc.Add(table);
}
出现的问题是,当我点击下载按钮时,第一个教员的PDF文件正在显示,即使我点击了其他教员的下载按钮,我也需要获取每个教员的数据。如何获取PDF文件
我的要求是,应为特定FID下载特定pdf,而不应仅为第一个FID下载。当您将FID设置到会话中时。一旦用户点击下载按钮,同时函数被调用。它从第一行开始,将采用第一个ID。现在它将被分配到会话变量中,并且每次单击都会重复相同的过程。您必须通过GridView的RowDataBound来管理事件。将FID设置到会话中时。一旦用户点击下载按钮,同时函数被调用。它从第一行开始,将采用第一个ID。现在它将被分配到会话变量中,并且每次单击都会重复相同的过程。您必须通过GridView的RowDataBound来管理事件。在itemtemplate中
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"
Width="294px" onselectedindexchanged="GridView1_SelectedIndexChanged"
>
<Columns>
<asp:BoundField DataField="FID" HeaderText="FID" SortExpression="FID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" runat="server"
CommandArgument="<("Value")>" OnClick="DownloadFile"
Text="Download" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ProjectConnectionString %>"
SelectCommand="SELECT [FID], [Name] FROM [Personal_det]">
</asp:SqlDataSource>
这行代码可能有助于解决您在itemtemplate中遇到的问题
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"
Width="294px" onselectedindexchanged="GridView1_SelectedIndexChanged"
>
<Columns>
<asp:BoundField DataField="FID" HeaderText="FID" SortExpression="FID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" runat="server"
CommandArgument="<("Value")>" OnClick="DownloadFile"
Text="Download" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ProjectConnectionString %>"
SelectCommand="SELECT [FID], [Name] FROM [Personal_det]">
</asp:SqlDataSource>
这行代码可能有助于解决您的问题您可以参考。您可以参考。
protected void DownloadFile(object sender, EventArgs e)
{
string FID = Convert.ToString(((LinkButton)sender).CommandArgument);
DataRow dr = GetData("select * from Personal_det where FID = '"+ FID +"'").Rows[0];
.....
//other codes
}