网格视图在asp.net表打印 [英] grid view printing with table in asp.net
问题描述
我使用的Visual Studio 2008和SQL Server 2008
i am using visual studio 2008 and sql server 2008
我要打印我的的GridView以表用一个按钮
在asp.net 3.5
code有三个部分首先,这是从我的默认页
i want to print my "gridview with table" using a button in asp.net 3.5 code has three part first this is from my default page
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=300px,width=300px,scrollbars=1');</script>");
PrintHelper.PrintWebControl(Panel1);
}
这code这个从打印页
this code this from print page
protected void Page_Load(object sender, EventArgs e)
{
Control ctrl = (Control)Session["ctrl"];
PrintHelper.PrintWebControl(ctrl);
}
这是我的打印辅助类
and this is my print helper class
public PrintHelper()
{
}
public static void PrintWebControl(Control ctrl)
{
PrintWebControl(ctrl, string.Empty);
}
public static void PrintWebControl(Control ctrl, string Script)
{
StringWriter stringWrite = new StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
if (ctrl is WebControl)
{
Unit w = new Unit(100, UnitType.Percentage); ((WebControl)ctrl).Width = w;
}
Page pg = new Page();
pg.EnableEventValidation = false;
if (Script != string.Empty)
{
pg.ClientScript.RegisterStartupScript(pg.GetType(), "PrintJavaScript", Script);
}
HtmlForm frm = new HtmlForm();
pg.Controls.Add(frm);
frm.Attributes.Add("runat", "server");
frm.Controls.Add(ctrl);
pg.DesignerInitialize();
pg.RenderControl(htmlWrite);
string strHTML = stringWrite.ToString();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write(strHTML);
HttpContext.Current.Response.Write("<script>window.print();</script>");
HttpContext.Current.Response.End();
}
请帮我出
推荐答案
创建一个数据适配器,创建一个命令对象与Select查询,设置适配器的命令,这个命令对象,发出adapter.fill(数据),设置GridView控件的数据集和数据绑定在GridView的数据源。
Create a Data Adapter, create a Command Object with a Select query, set the adapter's command to this command object, issue an adapter.fill(dataset), set the data source of gridview as the dataset and databind the GridView.
有关code,一个简单的谷歌搜索会工作。你甚至没有指定语言。我猜你是工作在C#,这是你的大学项目。是吗?
For code, a simple Google search will work. You did not even specify a language. I guess you are working in C# and this is your college project. Is it?
这篇关于网格视图在asp.net表打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!