在ItemTemplate字段中使用C#转换为PDF时出现问题 [英] Problem converting into PDF with C# at ItemTemplate field

查看:62
本文介绍了在ItemTemplate字段中使用C#转换为PDF时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做学校作业,但似乎无法在ItemTemplate下检索文本框值.

I am working on a school assignment but I couldn't seem to retrieve the textbox value under ItemTemplate.

这是我的代码:

ASPX:

<Columns>
    <asp:BoundField DataField="productName"
        HeaderText="Item name" />
    <asp:BoundField DataField="productLeadTime" HeaderText="Estimated arrival time" />
    <asp:TemplateField HeaderText="Quantity">
        <ItemTemplate>
            <asp:TextBox ID="tb_Quantity" runat="server" Text='<%# Eval("quantity") %>'
                Height="22px" Width="72px" OnTextChanged="tb_Quantity_TextChanged" AutoPostBack="True" DataField="quantity"></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="productPrice" DataFormatString="{0:C}" HeaderText="Unit Price" />
    <asp:BoundField DataField="TotalPrice" DataFormatString="{0:C}" HeaderText="Total price" />
</Columns>

ASPX.CS:

PdfPTable pdfTable = new PdfPTable(gvCartView.HeaderRow.Cells.Count);
foreach (TableCell gridViewHeaderCell in gvCartView.HeaderRow.Cells)
{
    Font font = new Font();
    font.Color = new BaseColor(0, 0, 0);

    PdfPCell pdfCell = new PdfPCell(new Phrase(gridViewHeaderCell.Text, font));
    //pdfCell.BackgroundColor = new BaseColor(gvCartView.HeaderStyle.BackColor);
    pdfTable.AddCell(pdfCell);
}

foreach (GridViewRow gridViewRow in gvCartView.Rows)
{
    foreach (TableCell gridViewCell in gridViewRow.Cells)
    {
        Font font = new Font();
        font.Color = new BaseColor(gvCartView.RowStyle.ForeColor);

        PdfPCell pdfCell = new PdfPCell(new Phrase(gridViewCell.Text, font));
        pdfCell.BackgroundColor = new BaseColor(gvCartView.RowStyle.BackColor);
        pdfTable.AddCell(pdfCell);
    }
}
Document pdfDocument = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
pdfDocument.SetPageSize(PageSize.A4.Rotate());
PdfWriter.GetInstance(pdfDocument, new FileStream(Server.MapPath("~/PDFDocuments/PO.pdf"), FileMode.Create));

pdfDocument.Open();
pdfDocument.Add(pdfTable);
pdfDocument.Close();

PDF输出结果

请协助...我曾尝试将Javascript与 OnClientClick 一起使用,但无法正常工作.您能为我迈出新的一步吗?也许我错误地执行了JavaScript方法.谢谢!

Please assist... I have tried with Javascript with OnClientClick but couldn't work. Can you provide me with a new step? Maybe I did the JavaScript method wrongly. Thank you!

推荐答案

您需要检查单元格的 ContainingField 类型是否为 TemplateField .如果是这样,请通过 FindControl ,否则获取已用于 BoundField 的单元格的文本:

You need to check type of ContainingField of cell is TemplateField. If so find textbox for Quantity via FindControl, otherwise get text of cell as you have already used for BoundField's:

foreach (GridViewRow gridViewRow in gvCartView.Rows)
{
    foreach (TableCell gridViewCell in gridViewRow.Cells)
    {
        Font font = new Font();
        font.Color = new BaseColor(gvCartView.RowStyle.ForeColor);
        var gridText = "";
        if (((DataControlFieldCell)gridViewCell).ContainingField is TemplateField)
        {
            if (((DataControlFieldCell)gridViewCell).ContainingField.HeaderText == "Quantity")
            {
                gridText = (gridViewCell.FindControl("tb_Quantity") as TextBox).Text;
            }
        }
        else
        {
            gridText = gridViewCell.Text;
        }
        PdfPCell pdfCell = new PdfPCell(new Phrase(gridText, font));
        pdfCell.BackgroundColor = new BaseColor(gvCartView.RowStyle.BackColor);
        pdfTable.AddCell(pdfCell);
    }
}

这篇关于在ItemTemplate字段中使用C#转换为PDF时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆