cancel
Showing results for 
Search instead for 
Did you mean: 

CrystalReportViewer.SearchAndHighlightText How it works?

Former Member
0 Kudos

While I was examining the CrystalReportViewer ASP.Net control, I found the SearchAndHighlightText method.

I used it in an example and found that it searches in the current page only, and highlights the matched text objects in it.

I want to create search functionality similar to that in word processors. Where a user searches for text, and the matches are highlighted, and "next-previous" buttons appear to allow the user to navigate through the matches. But SearchAndHighlightText only returns a bool not matches.

How may I create the required functionality if possible?

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

Hi Ashraf,

Works for me. You never said what Version of the SDK you are using? I'm using CR for VS SP 10.

Don

PS - moved your post to the CR for VS forums.

0 Kudos

Create your own button for searching when the viewer is active and the report is displayed. Then from your Text prompt UI pass the text into the YourTextString variable:

protected void Button1_Click(object sender, EventArgs e)

{

    CrystalReportViewer1.SearchAndHighlightText(YourTextString, CrystalDecisions.Shared.SearchDirection.Forward);

}

Then create the same for the Backward direction button:

protected void Button2_Click(object sender, EventArgs e)

{

    CrystalReportViewer1.SearchAndHighlightText(YourTextString, CrystalDecisions.Shared.SearchDirection.Backward);

}

You do need to use the PostBack instructions so you keep the report in session. Search and you'll find sample on how to use the Postback method.

Don

Former Member
0 Kudos

I think that this is the case. When I navigate back or forward I find that the highlights are removed.

But this utterly devalue the SearchAndHighlightText function, as I can do the search locally in the DOM objects using JavaScript and highlight the results.

Message was edited by: Don Williams

Former Member
0 Kudos

I created a setup similar to what you posted but for the forward direction only. And it works as I mentioned in the question. Regardless of how many I click the search button, the viewer doesn't advance to the next results. It stays on the same page. And when I navigate to a page that has the search word, I find it's not highlighted.

0 Kudos

Hi Ashraf,

Yes I deleted the post because it was not right under this circumstance...

I changed a my report and I find the same thing you do. If I start from a page that does not have the text it will go to a page with the string but it does not advance if I click the button again and the backward API still goes forward.

I'll have to do some more research and testing and then likely ping the developers on how this should work or if it's a bug.

Thanks again

Don

0 Kudos

And to keep things clear I delete your paste also...

Curious, why don't you use the Search button provided in the viewer? It works much faster:

Thanks again

Don

Former Member
0 Kudos

The viewer's search button seems to advance to the next page. But this revealed a weird behaviour.

When I first open the report (I use the "Mountain Bikes" example from your SDK), the pager looks like this:

It says (1 of 1+) while the report has 11 pages. When I search for the word "bike", it highlights the results in the first page. When I click it again, it advances to page 2, and the pager looks like this:

After that, it remains of page 2 no matter how many I click the search button. Notice that the word "bike" exists in almost every page of this report.

ido_millet
Active Contributor
0 Kudos

Does that problem go away if you place the special field of 'Page N of M' on the report layout?

Former Member
0 Kudos

The report already had the "Page Number" field. I added "Page N of M", and when I saved, the designer informed me that the report was in the old format and will be upgraded to the new format. After doing so, now the report viewer doesn't display "1 of 1+" nor "2 of 2+". It displays current page index and pages count properly, but the problem still exists: The search button doesn't advance past page 2 when I search for the word "bike".

You may experiment with the report yourself. It's the "NET-CS2005_CRNET_CR115_Set_Viewer-Locale-by-Page" sample found in the SDK samples:

Crystal Reports for .NET SDK Samples - Business Intelligence (BusinessObjects) - SCN Wiki

0 Kudos

Move your code from the Page_Load to the Page_Init. then it should work for that part anyways. That is a known update to WEB app's, Standards were applied and if you search you'll find the industry recommends moving your code to the INIT page.

It's still an issue once on a page where the string is found and not advancing to the next one.

Ido, that won't fix it. I can duplicate the problem, you have to click the next page and then hit the button again to continue searching.

I'm still curious why you simply don't use the Search feature in the viewer? It works and is a lot faster than using your own button.

This is a low priority bug since the Viewers search function works. I'll still track it but not sure when we'll fix it.

Don

Former Member
0 Kudos

Don Williams wrote:

Move your code from the Page_Load to the Page_Init. then it should work for that part anyways. That is a known update to WEB app's, Standards were applied and if you search you'll find the industry recommends moving your code to the INIT page.

I'm still curious why you simply don't use the Search feature in the viewer? It works and is a lot faster than using your own button.

This is a low priority bug since the Viewers search function works. I'll still track it but not sure when we'll fix it.

Don

There seems to be some misunderstanding during this discussion. Since you instructed me to use the built in search feature here, all the messages afterwards are about the built in search feature.

I didn't write any code for the built in search. So I thought you meant the code that creates the ReportDocument and assigns it to the viewer. I moved this code to OnInit and the built in search worked well. Now it advances to the page where the search results are found. This part is now resolved.

As for SearchAndHighlightText, either it has a bug that has to be resolved, or SAP should specify clearly in the documentation that this method just searches in the current page and it's the developer's responsibility to keep track of the searched page index and advances (or retards) it.

Answers (0)