单击“打印”按钮后,Colspan网格视图将展开更多列

本文关键字:视图 网格 Colspan 打印 按钮 单击 | 更新日期: 2024-09-24 18:48:54

下面的网格视图有4列。

我已经合并了页脚中的一些列。

我在单击printbtn后遇到的问题是,网格视图在页脚中添加了两列。

因此,输出将是原始网格视图列加上网格视图页脚中的2个空列。

网格视图

<asp:GridView ID="grid" runat="server" ShowFooter="true" DataSourceID="SqlDs_Grid" OnRowDataBound="grid_RowDataBound" AutoGenerateColumns="false" CssClass="table">
<Columns>
    <asp:BoundField HeaderText="Sr.No"></asp:BoundField>
    <asp:BoundField DataField="ProductName" HeaderText="Item Description" SortExpression="ProductName"></asp:BoundField>
    <asp:BoundField DataField="ProductQty" HeaderText="Quantity" SortExpression="ProductQty"></asp:BoundField>
    <asp:BoundField DataField="UnitPrice" HeaderText="Unit Price"></asp:BoundField>
    <asp:BoundField HeaderText="Total Amount" />
</Columns>

代码背后

protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells.RemoveAt(1);
            e.Row.Cells.RemoveAt(2);
            e.Row.Cells[0].ColumnSpan = 3;
            e.Row.Cells[0].Text = "<div class='pull-left'>Total Amount In  Words </div> <br>" + "<div class='pull-left'>" + NumberToWords(Convert.ToInt32(Totalamount)) + "Only</div>";
            e.Row.Cells[1].Text = "Total Amount:";
            e.Row.Cells[2].Text = Totalamount.ToString();
        }
}

打印按钮

<asp:Button ID="printbtn" runat="server" Text="Print" CssClass="btn btn-primary hidden-print" OnClientClick="javascript:window.print();" />

单击“打印”按钮后,Colspan网格视图将展开更多列

发生此问题的原因是单击按钮会导致回发。为了防止这种情况,在OnClientClient事件中返回false

<asp:Button ... OnClientClick="javascript:window.print(); return false;" />