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

时间:2012-03-11 08:03:31

标签: c# asp.net

我正在开发一个内部网Web应用程序,作为一个建议框。管理员将在GridView中看到所有已提交建议的列表,并且他有能力接受或拒绝这些建议。 在这个GridView下,还有另一个GridView,它显示了状态为Accepted或Rejected的建议。管理员想要打印第二个GridView。我跟着this Tutorial关于打印任何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>

1 个答案:

答案 0 :(得分:2)

我认为当你说“无法打印”时,你的意思是你的javascript没有运行。尝试以下内容:

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