Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从自定义按钮而不是工具栏打印ReportViewer_C#_Javascript_Asp.net_Printing_Reportviewer - Fatal编程技术网

C# 从自定义按钮而不是工具栏打印ReportViewer

C# 从自定义按钮而不是工具栏打印ReportViewer,c#,javascript,asp.net,printing,reportviewer,C#,Javascript,Asp.net,Printing,Reportviewer,好的,我花了几个小时(确切地说是4个小时)寻找解决方案。我发现了一些结果,但到目前为止没有一个是有效的( 问题:我在用户控件中有一个reportviewer,我隐藏了工具栏并创建了自己的工具栏。现在我添加了一个应该打印的按钮,但似乎无法使其工作。我将为大家提供任何解决方案。但它必须是一个按钮,而不是报表附带的默认栏 这是我的密码: <rsweb:reportviewer ID="rvReports" runat="server" Height="600px" Width="600px

好的,我花了几个小时(确切地说是4个小时)寻找解决方案。我发现了一些结果,但到目前为止没有一个是有效的(

问题:我在用户控件中有一个reportviewer,我隐藏了工具栏并创建了自己的工具栏。现在我添加了一个应该打印的按钮,但似乎无法使其工作。我将为大家提供任何解决方案。但它必须是一个按钮,而不是报表附带的默认栏

这是我的密码:

<rsweb:reportviewer 
ID="rvReports" 
runat="server" 
Height="600px"
Width="600px"
ShowToolBar="False"
SizeToReportContent="True" AsyncRendering="false" />

<asp:ImageButton ID="btnprint" runat="server" ImageUrl="../img/print.png" 
     OnClientClick="PrintReport();" />
为了以防万一,我添加了一个Jquery库,因为我以为这就是它,但什么也做不了。。
顺便说一句,我从另一个stackoverflow问题的答案中得到了javascript。

从错误中,它看起来像是
$find(“rvReports”)
返回没有控件的集合,结果get\u reportAreaContentType报告一个错误。在脚本调试器中检查
$find(“rvReports”)的结果是什么
调用并查看视图中是否存在您期望的元素


注意:我认为选择器应该是“#rvReports”…

我最终使用了ITextSharp,这非常简单。添加一个隐藏的iframe,并在代码后面:

Warning[] warnings;
        string[] streamids;
        string mimeType;
        string encoding;
        string extension;

        byte[] bytes = rvReports.LocalReport.Render("PDF", null, out mimeType,
                       out encoding, out extension, out streamids, out warnings);

        FileStream fs = new FileStream(HttpContext.Current.Server.MapPath("output.pdf"), FileMode.Create);
        fs.Write(bytes, 0, bytes.Length);
        fs.Close();

        //Open exsisting pdf
        Document document = new Document(PageSize.LETTER_LANDSCAPE, 0, 0, 0, 0);
        PdfReader reader = new PdfReader(HttpContext.Current.Server.MapPath("output.pdf"));
        //Getting a instance of new pdf wrtiter
        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(
           HttpContext.Current.Server.MapPath("Print.pdf"), FileMode.Create));
        document.Open();
        PdfContentByte cb = writer.DirectContent;

        int i = 0;
        int p = 0;
        int n = reader.NumberOfPages;
        Rectangle psize = reader.GetPageSize(1);

        //float width = psize.Width;
        //float height = psize.Height;

        //Add Page to new document
        while (i < n)
        {
            document.NewPage();
            p++;
            i++;

            PdfImportedPage page1 = writer.GetImportedPage(reader, i);
            cb.AddTemplate(page1, 0, 0);
        }

        //Attach javascript to the document
        PdfAction jAction = PdfAction.JavaScript("this.print(true);\r", writer);
        writer.AddJavaScript(jAction);
        document.Close();

        //Attach pdf to the iframe
        frmPrint.Attributes["src"] = "Print.pdf";
Warning[]警告;
字符串[]流线;
字符串模拟类型;
字符串编码;
字符串扩展;
byte[]bytes=rvReports.LocalReport.Render(“PDF”),null,out mimeType,
输出编码、输出扩展、输出流ID、输出警告);
FileStream fs=newfilestream(HttpContext.Current.Server.MapPath(“output.pdf”)、FileMode.Create);
fs.Write(字节,0,字节.长度);
fs.Close();
//开放式现有pdf
文档=新文档(PageSize.LETTER_,0,0,0);
PdfReader=newpdfReader(HttpContext.Current.Server.MapPath(“output.pdf”);
//获取新pdf文件的实例
PdfWriter writer=PdfWriter.GetInstance(文档,新文件流(
HttpContext.Current.Server.MapPath(“Print.pdf”)、FileMode.Create);
document.Open();
PdfContentByte cb=writer.DirectContent;
int i=0;
int p=0;
int n=reader.NumberOfPages;
矩形psize=reader.GetPageSize(1);
//浮动宽度=psize.宽度;
//浮动高度=psize.高度;
//将页面添加到新文档
而(i

就这样,guess不得不继续在google内部挖掘..lol

在$find函数中使用clientId而不是serverId:

$find(<%=rvReports.ClientID%>)
$find()

您的标题是“来自codebehind”(服务器端C#代码),但示例只有JavaScript客户端代码。哪一个是正确的(如果需要,请更新问题/标题)。好了,很抱歉。
Warning[] warnings;
        string[] streamids;
        string mimeType;
        string encoding;
        string extension;

        byte[] bytes = rvReports.LocalReport.Render("PDF", null, out mimeType,
                       out encoding, out extension, out streamids, out warnings);

        FileStream fs = new FileStream(HttpContext.Current.Server.MapPath("output.pdf"), FileMode.Create);
        fs.Write(bytes, 0, bytes.Length);
        fs.Close();

        //Open exsisting pdf
        Document document = new Document(PageSize.LETTER_LANDSCAPE, 0, 0, 0, 0);
        PdfReader reader = new PdfReader(HttpContext.Current.Server.MapPath("output.pdf"));
        //Getting a instance of new pdf wrtiter
        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(
           HttpContext.Current.Server.MapPath("Print.pdf"), FileMode.Create));
        document.Open();
        PdfContentByte cb = writer.DirectContent;

        int i = 0;
        int p = 0;
        int n = reader.NumberOfPages;
        Rectangle psize = reader.GetPageSize(1);

        //float width = psize.Width;
        //float height = psize.Height;

        //Add Page to new document
        while (i < n)
        {
            document.NewPage();
            p++;
            i++;

            PdfImportedPage page1 = writer.GetImportedPage(reader, i);
            cb.AddTemplate(page1, 0, 0);
        }

        //Attach javascript to the document
        PdfAction jAction = PdfAction.JavaScript("this.print(true);\r", writer);
        writer.AddJavaScript(jAction);
        document.Close();

        //Attach pdf to the iframe
        frmPrint.Attributes["src"] = "Print.pdf";
$find(<%=rvReports.ClientID%>)