در این آموزش asp net به نحوه چاپ GridView با قابلیت صفحه بندی و تکرار عنوان در هر صفحه پرداخته می شود . GridView یکی از کامپوننت های کاربردی در ASP.NET Form می باشد. که در این مثال کاربردی با آن بیشتر آشنا خواهیم شد .
یک GridView با یک button به صفحه اضافه کنید مانند کد های زیر
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Font-Names="Arial" Font-Size="11pt" AlternatingRowStyle-BackColor="#C2D69B" HeaderStyle-BackColor="green" AllowPaging="false"> <Columns> <asp:BoundField ItemStyle-Width="150px" DataField="CustomerID" HeaderText="CustomerID" /> <asp:BoundField ItemStyle-Width="150px" DataField="City" HeaderText="City" /> <asp:BoundField ItemStyle-Width="150px" DataField="PostalCode" HeaderText="PostalCode" /> </Columns> </asp:GridView> <asp:Button ID="btnPrint" runat="server" Text="Print" OnClick="Print" />
این کدها را قسمت رویداد دکمه چاپ بنوسید:
protected void Print(object sender, EventArgs e) { GridView1.UseAccessibleHeader = true; GridView1.HeaderRow.TableSection = TableRowSection.TableHeader; GridView1.FooterRow.TableSection = TableRowSection.TableFooter; GridView1.Attributes["style"] = "border-collapse:separate"; foreach (GridViewRow row in GridView1.Rows) { if (row.RowIndex % 10 == 0 && row.RowIndex != 0) { row.Attributes["style"] = "page-break-after:always;"; } } StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); GridView1.RenderControl(hw); string gridHTML = sw.ToString().Replace("\"", "'").Replace(System.Environment.NewLine, ""); StringBuilder sb = new StringBuilder(); sb.Append("<script type = 'text/javascript'>"); sb.Append("window.onload = new function(){"); sb.Append("var printWin = window.open('', '', 'left=0"); sb.Append(",top=0,width=1000,height=600,status=0');"); sb.Append("printWin.document.write(\""); string style = "<style type = 'text/css'>thead {display:table-header-group;} tfoot{display:table-footer-group;}</style>"; sb.Append(style + gridHTML); sb.Append("\");"); sb.Append("printWin.document.close();"); sb.Append("printWin.focus();"); sb.Append("printWin.print();"); sb.Append("printWin.close();"); sb.Append("};"); sb.Append("</script>"); ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", sb.ToString()); GridView1.DataBind(); }
سلام در قسمت GridView1.RenderControl(hw); موقعي كه print ميزنم خطا اين ميده
An exception of type ‘System.Web.HttpException’ occurred in System.Web.dll but was not handled in user code
Additional information: Control ‘GridView1’ of type ‘GridView’ must be placed inside a form tag with runat=server.
چيكارش كنم باتشكر