简体   繁体   English

从gridview访问数据以在asp.net中下载PDF文档

[英]Accessing the data from the gridview to download a PDF document in asp.net

     FID            NAME    
     M001           Faculty1    download
     M002           Faculty2    download
     M003           Faculty3    download

The above shown is my grid view containing details of faculty and the download link indicates the pdf form of the faculty to be downloaded. 上面显示的是我的网格视图,其中包含教师的详细信息,并且下载链接指示要下载的教师的pdf形式。

       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);
 }

My grid view code is shown below 我的网格视图代码如下所示

           <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="&lt;(&quot;Value&quot;)&gt;" 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>

The problem arises is whenver i click the download button the PDF of 1st faculty is been displaying even if i click download button for other faculty also,I need to get data for every faculty How to do get the pdf? 出现的问题是,即使我也单击其他教师的下载按钮,只要我单击下载按钮,就会显示第一教师的PDF,我需要获取每个教师的数据。如何获取pdf?

My requirement is that the paricular pdf should be downloaded for the particular FID and it shouldnot download only for the first FID. 我的要求是,应为特定的FID下载特殊的pdf文件,而不应仅为第一个FID下载。

Inside you itemtemplate 在你里面itemtemplate

<asp:LinkButton ID="lnkDownload" runat="server" 
  CommandArgument='<%#Eval("FID")%>' CommandName="DownloadFile" OnClick="DownloadFile" Text="Download" />

In .cs 在.cs中

 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
  }

May this line of code will help solving your problem 愿这一行代码可以帮助您解决问题

As you are setting the FID into the session. 在将FID设置为会话时。 As soon as User clicks on Download button, at the same time the function getting called. 用户单击“下载”按钮后,将同时调用该函数。 Its starting from very first row and will take first ID. 它从第一行开始,将使用第一个ID。 Now it will be assigned in the session variable and same process is repeating for each click. 现在,它将在会话变量中分配,并且每次点击都重复相同的过程。 You have to manage the event through RowDataBound of the GridView. 您必须通过GridView的RowDataBound管理事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM