views:

1504

answers:

4

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.

+2  A: 

See this MSDN page (SQL 2005 version, but 2008 is the same, I believe).

Ed Harper
A: 

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

Rob Farley
That is a very interesting solution. It would workin many situations. My particular prolem is a 25 page report. The item may be on page 15. If I could jump to the page too (like find does) that would be perfect.
Praesagus
I'd've thought the ReportViewer control would have a member for providing a FindString (either a Method you call immediately following the Refresh command, or a Property you can set beforehand).
Rob Farley
+1  A: 

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">
Noah
My report viewer object does not seem to have a 'find' method. Do I need more than the Microsoft.Reporting.WebForms reference?
Praesagus
The find method is in Microsoft.Reporting.WinForms, so this may not work for you. How are you displaying the report? Is it on a webpage? What does the code look like?
Noah
I added code to the question. How can I set up a reference to Microsoft.Reporting.WinForms?
Praesagus
+2  A: 

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!

klabranche
I will try this, thank you. Stackoverflow automatically accepted Ed's answer before I got to see yours because of the bounty...grrr.
Praesagus