1

这是我在 aspx 中的网格视图代码:

    <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='<%Eval("FID")%>' 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>

这是网格视图的显示方式:

 FID            NAME    
 M001           Faculty1    download
 M002           Faculty2    download
 M003           Faculty3    download

如您所见,下面提到的是我的 C# 代码,用于根据他们的 FID 下载教师的特定信息,但此代码不返回任何内容。

protected void DownloadFile(object sender, EventArgs e)
{
    string FID = ((LinkButton)sender).CommandArgument;


    var dt = GetData("select * from Personal_det where FID='" + FID + "'");

    if (dt != null && dt.Rows.Count > 0)
    {
        DataRow dr = dt.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);

            //Education Details
            PdfPTable tabl = new PdfPTable(2);
            tabl.SpacingBefore = 10f;
            bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
            times = new Font(bfTimes, 12f, Font.UNDERLINE);
            para = new Paragraph("Education Details:", times);
            para.Alignment = Element.ALIGN_LEFT;
            doc.Add(para);
            //tabl.BorderWidth=1;
            //tabl.BorderColor=new BaseColor(0,0,255);
            //cell = new PdfPCell(new Phrase("Education Detail"));
            //cell.Headers=true;
            cell.HorizontalAlignment = 1;
            cell.Colspan = 4;
            cell.Rowspan = 2;
            cell.BorderColor = new BaseColor(0, 0, 0);
            DataRow dr1 = GetData("select * from Education_det ").Rows[0];
            tabl.AddCell(new Phrase("Degree:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)));
            tabl.AddCell(new Phrase(dr1["Degree"].ToString(), FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)));
            tabl.AddCell(new Phrase("Year:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)));
            tabl.AddCell(new Phrase(Convert.ToInt32(dr1["year"]).ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
            tabl.AddCell(new Phrase("Institution:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)));
            tabl.AddCell(new Phrase(dr1["Institution"].ToString(), FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)));
            tabl.AddCell(new Phrase("Specialization:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)));
            tabl.AddCell(new Phrase(dr1["Specialization"].ToString(), FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)));
            cell.Rowspan = 2;
            cell.Colspan = 2;
            tabl.AddCell(cell);
            tabl.SpacingAfter = 30f;
            doc.Add(tabl);
 }
4

2 回答 2

2

尝试这个:

ASPX:

<asp:TemplateField ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                 <asp:LinkButton ID="lnkDownload" runat="server" 
                     CommandArgument='<%Eval("FID")%>' OnClick="DownloadFile"
                      Text="Download" />
            </ItemTemplate>
        </asp:TemplateField>

后面的代码:

protected void DownloadFile(object sender, EventArgs e)
{
    string FID = ((LinkButton)sender).CommandArgument;
    var dt = GetData("select * from Personal_det where Fid='" + FID + "'");
    //and so on
}
于 2013-09-28T04:40:23.487 回答
0

使用它,将此代码添加到您的 aspx 页面中

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
    Width="294px" OnRowCommand="GridView1_RowCommand">
    <Columns>
        <asp:TemplateField ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:LinkButton ID="lnkDownload" runat="server" CommandArgument='<%Eval("FID")%>'
                    CommandName="DownloadFile" Text="Download" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

然后在后面添加代码(aspx.cs)

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "DownloadFile")
    {
            //write your code here
    }
}

您正在使用 create PDFusing ItextSharp。 在这个你被遗忘doc.Close();之后添加然后它的工作。doc.Open();doc.Add(tabl);doc.Close();

于 2013-10-01T04:54:14.513 回答