簡體   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

上面顯示的是我的網格視圖,其中包含教師的詳細信息,並且下載鏈接指示要下載的教師的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);
 }

我的網格視圖代碼如下所示

           <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>

出現的問題是,即使我也單擊其他教師的下載按鈕,只要我單擊下載按鈕,就會顯示第一教師的PDF,我需要獲取每個教師的數據。如何獲取pdf?

我的要求是,應為特定的FID下載特殊的pdf文件,而不應僅為第一個FID下載。

在你里面itemtemplate

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

在.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
  }

願這一行代碼可以幫助您解決問題

在將FID設置為會話時。 用戶單擊“下載”按鈕后,將同時調用該函數。 它從第一行開始,將使用第一個ID。 現在,它將在會話變量中分配,並且每次點擊都重復相同的過程。 您必須通過GridView的RowDataBound管理事件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM