简体   繁体   中英

Sql Reporting services - find item in report - on load

SQL reporting services has a little search box in the top of the report viewer. 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. Is there a method that will find? I am using sql 2008 express and 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.


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.


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:

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. 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. The second number (4) could vary based on what you are showing / not showing in the toolbar. 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:

<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. You will notice the ctl01 & ctl04 and ctl00 follow the same numbering as the server side code. 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.

I then set the onload event for the body of the page to the javascript function:

<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. 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.

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. :)

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:

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.

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:

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:

<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. ;)

Rob

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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