如何修复此代码以打印此GridView控件

本文关键字:GridView 控件 打印 代码 何修复 | 更新日期: 2023-09-27 18:27:19

我正在开发一个intranet web应用程序,它可以作为建议框使用。管理员将在GridView中看到所有提交的建议的列表,他可以接受或拒绝这些建议。在此GridView下,还有另一个GridView,它显示状态为"已接受"或"已拒绝"的建议。管理员想要打印第二个GridView。我遵循了本教程中关于打印任何ASP.NET控件的内容,一切都很好。

我的ASP.NET代码:

<asp:Panel ID="Panel1" runat="server">
                        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
                                      AllowSorting="True" CssClass="mGrid"
                                      AlternatingRowStyle-CssClass="alt" 
                                      RowStyle-HorizontalAlign="Center" 
                                      DataSourceID="SqlDataSource4">
                            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                            <HeaderStyle Font-Bold = "true" ForeColor="Black" Height="20px"/> 
                            <Columns>
                                <asp:BoundField DataField="SubmittedMonth" HeaderText="Submitted Month" 
                                    SortExpression="SubmittedMonth" ReadOnly="True" />
                                <asp:BoundField DataField="DivisionShortcut" HeaderText="Division" 
                                    SortExpression="DivisionShortcut" />
                                <asp:BoundField DataField="Username" HeaderText="Username" 
                                    SortExpression="Username" />
                                <asp:BoundField DataField="Name" HeaderText="Name" 
                                    SortExpression="Name" />
                                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                                <asp:BoundField DataField="Description" HeaderText="Description" 
                                    SortExpression="Description" />
                                <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
                                <asp:BoundField DataField="Status" HeaderText="Status" 
                                    SortExpression="Status" />
                            </Columns>
                                <RowStyle HorizontalAlign="Center"></RowStyle>
                        </asp:GridView>
                        </asp:Panel>
                        <asp:SqlDataSource ID="SqlDataSource4" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT     TOP (100) PERCENT LEFT(DATENAME(month, dbo.SafetySuggestionsLog.DateSubmitted), 3) + '-' + DATENAME(year, dbo.SafetySuggestionsLog.DateSubmitted) 
                      AS SubmittedMonth, dbo.Divisions.DivisionShortcut, dbo.SafetySuggestionsLog.Username, dbo.employee.Name, dbo.SafetySuggestionsLog.Title, 
                      dbo.SafetySuggestionsLog.Description, dbo.SafetySuggestionsType.Type, dbo.SafetySuggestionsStatus.Status
FROM         dbo.Divisions INNER JOIN
                      dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode INNER JOIN
                      dbo.SafetySuggestionsLog ON dbo.employee.Username = dbo.SafetySuggestionsLog.Username INNER JOIN
                      dbo.SafetySuggestionsType ON dbo.SafetySuggestionsLog.TypeID = dbo.SafetySuggestionsType.ID INNER JOIN
                      dbo.SafetySuggestionsStatus ON dbo.SafetySuggestionsLog.StatusID = dbo.SafetySuggestionsStatus.ID
WHERE     (DATEDIFF(month, dbo.SafetySuggestionsLog.DateSubmitted, GETDATE()) &lt; 3)
ORDER BY dbo.SafetySuggestionsLog.DateSubmitted DESC">
                       </asp:SqlDataSource>
                       <asp:Button ID="btnPrint" runat="server" Text="Print" OnClick="btnPrint_Click" />

代码背后:

protected void btnPrint_Click(object sender, EventArgs e)
    {
        Session["ctrl"] = Panel1;
        ClientScript.RegisterStartupScript(this.GetType(), "onclick", "<script language=javascript>window.open('Print.aspx','PrintMe','height=400px,width=800px,scrollbars=1');</script>");
    }

我可以打印这个表,但当我把它放在UpdatePanel控件中时,我无法打印它,我不知道为什么有什么想法吗?有什么帮助吗

<asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="ddlDivision" EventName="SelectedIndexChanged" />
    </Triggers>
    <ContentTemplate>
            <br />
                Safety Suggestions List (for the last three months)
                        <br />
                        <asp:Panel ID="Panel1" runat="server">
                        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
                                      AllowSorting="True" CssClass="mGrid"
                                      AlternatingRowStyle-CssClass="alt" 
                                      RowStyle-HorizontalAlign="Center" 
                                      DataSourceID="SqlDataSource4">
                            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                            <HeaderStyle Font-Bold = "true" ForeColor="Black" Height="20px"/> 
                            <Columns>
                                <asp:BoundField DataField="SubmittedMonth" HeaderText="Submitted Month" 
                                    SortExpression="SubmittedMonth" ReadOnly="True" />
                                <asp:BoundField DataField="DivisionShortcut" HeaderText="Division" 
                                    SortExpression="DivisionShortcut" />
                                <asp:BoundField DataField="Username" HeaderText="Username" 
                                    SortExpression="Username" />
                                <asp:BoundField DataField="Name" HeaderText="Name" 
                                    SortExpression="Name" />
                                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                                <asp:BoundField DataField="Description" HeaderText="Description" 
                                    SortExpression="Description" />
                                <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
                                <asp:BoundField DataField="Status" HeaderText="Status" 
                                    SortExpression="Status" />
                            </Columns>
                                <RowStyle HorizontalAlign="Center"></RowStyle>
                        </asp:GridView>
                        </asp:Panel>
                        <asp:SqlDataSource ID="SqlDataSource4" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT     TOP (100) PERCENT LEFT(DATENAME(month, dbo.SafetySuggestionsLog.DateSubmitted), 3) + '-' + DATENAME(year, dbo.SafetySuggestionsLog.DateSubmitted) 
                      AS SubmittedMonth, dbo.Divisions.DivisionShortcut, dbo.SafetySuggestionsLog.Username, dbo.employee.Name, dbo.SafetySuggestionsLog.Title, 
                      dbo.SafetySuggestionsLog.Description, dbo.SafetySuggestionsType.Type, dbo.SafetySuggestionsStatus.Status
FROM         dbo.Divisions INNER JOIN
                      dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode INNER JOIN
                      dbo.SafetySuggestionsLog ON dbo.employee.Username = dbo.SafetySuggestionsLog.Username INNER JOIN
                      dbo.SafetySuggestionsType ON dbo.SafetySuggestionsLog.TypeID = dbo.SafetySuggestionsType.ID INNER JOIN
                      dbo.SafetySuggestionsStatus ON dbo.SafetySuggestionsLog.StatusID = dbo.SafetySuggestionsStatus.ID
WHERE     (DATEDIFF(month, dbo.SafetySuggestionsLog.DateSubmitted, GETDATE()) &lt; 3)
ORDER BY dbo.SafetySuggestionsLog.DateSubmitted DESC">
                       </asp:SqlDataSource>
                       <asp:Button ID="btnPrint" runat="server" Text="Print" OnClick="btnPrint_Click" />

       </ContentTemplate>
    </asp:UpdatePanel>

如何修复此代码以打印此GridView控件

我认为,当您说"无法打印"时,意味着您的javascript没有运行。尝试以下操作:

ScriptManager.RegisterStartupScript(Gridview2, Gridview2.GetType(), "onclick", "window.open('Print.aspx','PrintMe','height=400px,width=800px,scrollbars=1');", true);