Maintain scroll position on Crystal Report Grouptree
I have a Crystal Report created in Visual Studio 2008 which displays various stock codes with their prices. The Group Tree for the Crystal Report Viewer is enabled & displays a list of the stock codes for easy navigation. The list in the Group Tree can become quite long (100+ stock codes), which causes a scroll bar to appear.
Problem is, when the user selects a stock code from the Group Tree, the page posts back & the scroll position on the Group Tree is lost. This is particularly frustrating, especially when you're working with stock codes that are towards the end of the list.
Is there any way that I could get the current scroll position of the Group Tree, save it & re-assign it after the report has reloaded?
Some things I have tried, but without much success:
The Group Tree is rendered as a div, but without an ID. So I've retrieved the Group Tree control from th开发者_开发问答e server side by using the Report Viewers Controls collection (it's the 4th control in the collection) & checked its UniqueID value. I then used that to call document.getElementById(), but this always returns null. Also, I'm unable to use the control I retrieve from server-side, as I can't find the class (CrystalDecisions.Web.CrystalReportGroupTree), which is the type supplied when calling GetType() on the Group Tree control.
Any assistance in this matter will be greatly appreciated.
So I've found a solution that works pretty well. I've created my own "Group Tree" using a ListBox
& positioned it next to the CrystalReportViewer
, hiding the viewers actual GroupTree
. The ListBox
maintains its own scroll position, as well as the current selection. The added bonus with this approach is that users can use the arrow keys to navigate the GroupTree
, as long as it has focus.
My method of implementation is as follows:
I've added a standard ListBox
control to the page & positioned it to the left of the CrystalReportViewer
. The markup is as follows:
<asp:ListBox ID="lstStockCode" runat="server" Width="185px" Height="760px" Font-Size="10pt" AutoPostBack="true" OnSelectedIndexChanged="lstStockCode_SelectedIndexChanged" DataTextField="StockCode" DataValueField="ID" BackColor="#E4E4EC" style="position: relative; top: 30px; border-width: 0px;" Visible="false"></asp:ListBox>
The CrystalReportViewer
is initially hidden, until the report criteria have been selected, at which point both the Viewer & ListBox
will become visible.
Next, bind the ListBox
to a data source that returns the same data as what the report is bound to. In my case, I had created a method to retrieve stock codes using the same SQL query as the Stored Procedure my report is bound to.
Lastly, search the displayed Report for the value selected in the ListBox
. Below is a sample of the SelectedIndexChanged Event:
protected void lstStockCode_SelectedIndexChanged(object sender, EventArgs e)
{
if (ViewState["PrevListIndex"] == null)
CrystalReportViewer1.SearchAndHighlightText(lstStockCode.SelectedItem.Text, CrystalDecisions.Shared.SearchDirection.Forward);
else if (Convert.ToInt32(ViewState["PrevListIndex"]) < lstStockCode.SelectedIndex)
CrystalReportViewer1.SearchAndHighlightText(lstStockCode.SelectedItem.Text, CrystalDecisions.Shared.SearchDirection.Forward);
else if (Convert.ToInt32(ViewState["PrevListIndex"]) > lstStockCode.SelectedIndex)
CrystalReportViewer1.SearchAndHighlightText(lstStockCode.SelectedItem.Text, CrystalDecisions.Shared.SearchDirection.Backward);
ViewState["PrevListIndex"] = lstStockCode.SelectedIndex;
CrystalReportViewer1.ToolbarStyle.Width = Unit.Parse("1096px");
lstStockCode.Focus();
}
Because the search direction needs to specified for the CrystalReportViewer
, I just store my current search index & determine whether I'm searching further down or back up in the report, seeing as my ListBox
is returned using the same query as my report & thus is in the same order as the report data.
A few things of interest about this implementation, mostly in terms of styling. The supplied code does not make provision for the repositioning of the CrystalReportViewer Toolbar
. While the ListBox
sits in line with the report, it only has whitespace above it where the Toolbar
would normally sit. Also, the TreeView-like dotted lines do not appear inside the ListBox
. I haven't played around with using a TreeView
control as opposed to a ListBox
to achieve the same effect but I believe it to be very doable.
Even with these differences in terms of look & feel, the client was unaware that a new control was built to achieve this goal.
精彩评论