简体   繁体   English

Sql Reporting服务 - 在报告中查找项目 - 加载时

[英]Sql Reporting services - find item in report - on load

SQL reporting services has a little search box in the top of the report viewer. SQL报告服务在报告查看器的顶部有一个小搜索框。 When used, it finds the search text, navigates to containing page and highlights the text on the page. 使用时,它会找到搜索文本,导航到包含页面并突出显示页面上的文本。 My question is how can I do this when the report loads. 我的问题是如何在报告加载时执行此操作。

Currently I have a reportviewer embedded in my page. 目前我的页面中嵌入了一个reportviewer。 Is there a method that will find? 有没有找到的方法? I am using sql 2008 express and Dot Net 2 我使用的是sql 2008 express和Dot Net 2

For example I send the serial number 1234 to the report so that when it opens it acts like the user searched for the text and finds it for them in the report. 例如,我将序列号1234发送到报告,以便在打开时,它就像用户搜索文本并在报告中找到它们一样。


Ed gave me the answer to the url part. 艾德给了我网址部分的答案。 http://server/Reportserver?/SampleReports/Product Catalog&rc:FindString=mystring but I still can't figure out the reportviewer. http://server/Reportserver?/SampleReports/Product Catalog&rc:FindString=mystring但我仍然无法弄清楚reportviewer。


Here is some of the page code: 以下是一些页面代码:

using Microsoft.Reporting.WebForms; 

protected void Page_Load(object sender, EventArgs e)

{
    if (!Page.IsPostBack)
    {
        Int32 iID = Convert.ToInt32(Request.QueryString["ID"]);
        String reportsPath = ConfigurationManager.AppSettings["ReportsPath"];
        String sReportName = "ReportInvoice";

        reportViewer1.Reset();
        reportViewer1.ProcessingMode = ProcessingMode.Remote;
        reportViewer1.ShowParameterPrompts = false;
        reportViewer1.ServerReport.ReportServerUrl = new Uri(ConfigurationManager.AppSettings["ReportViewerUrl"]);
        reportViewer1.ServerReport.ReportServerCredentials = new ReportServerCredentials();//http://localhost/reportserver
        reportViewer1.AsyncRendering = false;
        ReportParameter[] reportParams = new ReportParameter[1];
        reportViewer1.ServerReport.ReportPath = reportsPath + sReportName;
        reportParams[0] = new ReportParameter("invoiceID", iID.ToString());
        reportViewer1.ServerReport.Refresh();
    }
}

Thanks in advance. 提前致谢。

请参阅此MSDN页面 (SQL 2005版本,​​但我相信2008年是相同的)。

I read through alot of the MSDN articles on the web based report viewer and tried several ways to fire off the search but only found this one to work: 我在基于Web的报告查看器上阅读了很多MSDN文章,并尝试了几种方法来启动搜索,但只发现这个工作:

First, in code you can set the search text box like so: 首先,在代码中,您可以设置搜索文本框,如下所示:

    TextBox txt;
    txt = (TextBox) this.ReportViewer1.Controls[1].Controls[4].Controls[0];        
    txt.Text = "test";

I did it in the ReportViewer's PreRender event. 我是在ReportViewer的PreRender活动中完成的。 Position 1 in the first control list is the toolbar control, #4 is the search group control and then in that group the first control is the text box. 第一个控制列表中的位置1是工具栏控件,#4是搜索组控件,然后在该组中第一个控件是文本框。 The second number (4) could vary based on what you are showing / not showing in the toolbar. 第二个数字(4)可能会根据您在工具栏中显示/未显示的内容而有所不同。 I was working with the default report viewer settings. 我正在使用默认的报表查看器设置。 It's a hack but it works. 这是一个黑客但它的工作原理。

Then I tried firing off the search event myself but this didn't result in the search working although it did fire off the event and with the correct information.... 然后我尝试自己解雇搜索事件,但这并没有导致搜索工作,虽然它确实触发了事件和正确的信息....

So here's what I did. 所以这就是我做的。

I created a javascript function: 我创建了一个javascript函数:

<script type="text/javascript">
    function OnFirstLoad() {
        if (!isPostBack)
            document.getElementById('ReportViewer1').ClientController.ActionHandler('Search', document.getElementById('ReportViewer1_ctl01_ctl04_ctl00').value);
    }
</script>

I read the source of the .aspx page and found the text "find" and figured out what the client side call was. 我阅读了.aspx页面的来源,找到了文本“find”并找出了客户端调用的内容。 You will notice the ctl01 & ctl04 and ctl00 follow the same numbering as the server side code. 您会注意到ctl01&ctl04和ctl00遵循与服务器端代码相同的编号。 You would need to change this to reflect your code. 您需要更改此设置以反映您的代码。 Again the second one (ctl04) is the one that is likely to change depending on how your toolbar is setup. 同样,第二个(ctl04)可能会根据工具栏的设置方式而改变。

I then set the onload event for the body of the page to the javascript function: 然后我将页面主体的onload事件设置为javascript函数:

<body onload="OnFirstLoad();">

The last trick was to only call this code the first time. 最后一招是第一次只调用这段代码。 So I added this to the page load event of the form code behind: 所以我将此添加到后面的表单代码的页面加载事件中:

If (!IsPostBack)
    ClientScript.RegisterClientScriptBlock(GetType(), "IsPostBack", "var isPostBack = false;", true);
else
    ClientScript.RegisterClientScriptBlock(GetType(), "IsPostBack", "var isPostBack = true;", true);

This creates a variable that the javascript function checks. 这将创建一个javascript函数检查的变量。 On the first go round it's false so it calls the report viewers search function, otherwise it's true and doesn't fire. 在第一轮它是假的所以它调用报告查看器搜索功能,否则它是真的,不会触发。

This is a pretty bad hack in my opinion and fragile. 在我看来,这是一个非常糟糕的黑客,而且很脆弱。 Changes of the report viewer's toolbar settings may require changes to the javascript and the code to set the text box. 更改报表查看器的工具栏设置可能需要更改javascript和代码以设置文本框。

I created a report that had several pages and the first hit wasn't until the third page and it went straight to it. 我创建了一个包含多个页面的报告,第一次点击直到第三页才直接进入。 From there the next button worked great until the end of the report. 从那里开始,下一个按钮效果很好,直到报告结束。

To bad it's not as simple as the windows based report viewer or the server based report viewer. 糟糕的是,它并不像基于Windows的报表查看器或基于服务器的报表查看器那么简单。 :) :)

Good Luck! 祝好运!

If you're trying to do this in a form in code behind then you need to find the report viewer object and add an event to the RenderingComplete that implements Find, so something like this: 如果您尝试在代码后面的表单中执行此操作,则需要查找报表查看器对象并向实现Find的RenderingComplete添加事件,如下所示:

public Report()
{
    InitializeComponent();


    rpViewer.RenderingComplete += new RenderingCompleteEventHandler(rpViewer_RenderingComplete);

}

void rpViewer_RenderingComplete(object sender, RenderingCompleteEventArgs e)
{
    int x = rpViewer.Find("0", 1);
}

EDIT: 编辑:

So, since this in a webpage you can't use the WinForms Controls, however, I was able to wire up a little less hacked version using Javascript that klabranche had used. 因此,由于这在网页中你不能使用WinForms控件,但是,我能够使用klabranche使用的Javascript连接一个较少被黑客攻击的版本。

Here's a code behind class that adds a javascript function to the body of the html to search the report for the search text that you want: 这是一个类后面的代码,它将一个javascript函数添加到html的主体,以在报告中搜索您想要的搜索文本:

private void SearchReport(ReportViewer rv, string SearchText)
{
    TextBox txt = (TextBox)rv.Controls[1].Controls[4].Controls[0];
    txt.Text = SearchText;
    this.Body.Attributes.Add("onload", "javascript:document.getElementById('" + rv.ClientID + 
        "').ClientController.ActionHandler('Search', '" + SearchText + "');");
}

If you don't add the search text to the text box, then it won't show the search string in the text box on the report. 如果不将搜索文本添加到文本框,则它不会在报表的文本框中显示搜索字符串。 This also only works for one report, so if you had additional reports you'd need to alter this. 这也仅适用于一个报告,因此如果您有其他报告,则需要更改此报告。 Also, for this to work you need to alter the body tag of your html: 此外,为此,你需要改变你的html的body标签:

<body id="Body" runat="server">

Have a textbox in your report that uses an expression for its background, set something like: 在报表中有一个文本框,它使用表达式作为其背景,设置如下:

=iif(me.value = Parameters!Highlight.value, "Yellow", "White")

And of course, make a parameter called Highlight. 当然,制作一个名为Highlight的参数。 ;) ;)

Rob

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM