Friday, February 29, 2008

How to make your ReportViewer(Local Mode) display in FULL PAGE?

.Net Framework Used: .Net 2.0

Usually when you have created your report using Report Viewer, it has toolbars on top and scrollbars for both horizontal and vertical border. The report will be display inside the body of ReportViewer control and when you write it to printer to print it as image, the printer will render it as how it looks (With both scrollbars and you can not view the report in full).

To View it in full and remove those scrollbars, simply, set the properties of ReportViewer Control (AsyncRendering) to false:-

<rsweb:ReportViewer ID="ReportViewer1" AsyncRendering="false" runat="server" ...

Then it will display in full size in a page.

Create ASP .Net 2.0 ReportViewer (Local Mode)

.Net Framework Used: .Net 2.0
Other Components: Web Client Software Factory (WCSF), Repository Factory

How to create a ReportViewer using Local Mode?
1.) Create a report template file (.RDLC) in your website.
2.) Design your report template file by double click the test.rdlc
3.) In your report template, you may need to create the dataset so that it can be assigned to your report. To create a dataset, you need to create your store procedure first.
3.) In your aspx page (switch to design mode), drag and drop the ReportViewer control to your page.
4.) Create your datasource and bind to the reportviewer. In my example, It's using ObjectContainerDataSource. Do it in design mode, it will save a lot of times. Moreover, multiple datasources are possible.



1. Create a report template file (.RDLC)
Steps:-
1.)To Create a report template, open Solution Explorer and create a new folder under Development Website. Name the folder "Reports".
2.) Right Click on the Reports folder and click on Add New Item.
3.) In the Template window, select Report and click Add.




4.) you will notice a file named SampleReport.rdlc created under the Report folder.

2. Design your report template file & 3. Create DataSet and Assign it to your report template.
Steps:-
1.) Double click on SampleReport.rdlc to open the designer.
2.) In the designer, there is a toolbox on your left side. Open the ToolBox and drag the item you want into the report. For instance, drag a Table.
3.) Notice that there is a datasource window under the toolbox. That's the place where you create your dataset.

4.) Create your dataset by specifying your connection, Store Procedure and etc.
5.) After you have created your dataset, it will appear on your left panel in Website Data Source window.
6.) Expand it and drag the file to the place you want to place it on the report designer. For example, drag and place it in the Table object.
7.) After you have assigned the attribute, the report template creation is done.

4. Add ReportViewer control into your ASPX page
Steps:-
1.) Create an ASPX page if you do not have one.
2.) Open your Toolbox and drag the ReportViewer control to your ASPX page (Design mode).

3.) Once you have created the control on your aspx page, select it and click on the arrow on the top right corner to setup the control. For example, Bind it to your RDLC file.

4.) After you have selected a RDLC file, drag and drop an ObjectContainerDataSource control to your page. ObjectContainerDataSource requires the Microsoft.Practices.Web.UI.WebControls.dll to be added to the toolbox.
5.) Configure the ObjectContainerDataSource. For e.g Name and BusinessEntities. If you are using WCSF and Repository Factory to create your web pages, Business Entities has been created based on your Database.
6.) Now, Choose the Datasource for ReportViewer control and Match it to the right dataset.

7.) You have done the setup for your report viewer control, and Now, the final step is to provide the data to your DataSource in your code behind.

You may also refer to This Website as reference.

How to add External Javascript (.js) file to your ASPX page

I was having a problem fixing this and it took me 1 day time to search through the net and got it fix nicely.

.Net Framework used: 2.0
Language used: C#

To register the Javascript file to your page, you need to :
1.) Register it in your code behind
2.) If it has a function that need you to call from your page, you need to use OnClientClick event for your LinkButton/Button. Once you click then it will call the function in your .js file.


1. Register your external Javascript in Code Behind
Register your external Javascript in code behind by using RegisterClientScriptInclude() function. You may refer to http://msdn2.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerclientscriptinclude.aspx for details.

protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this._presenter.OnViewInitialized();
this.init();
}
this._presenter.OnViewLoaded();

String csname = "AreaPrint";
String csurl = "~/AreaPrintingService.js";
Type cstype = this.GetType();

ClientScriptManager cs = Page.ClientScript;

if (!cs.IsStartupScriptRegistered(cstype, csname))
{
cs.RegisterClientScriptInclude(cstype, csname, ResolveClientUrl(csurl));

}
}


2. OnClientClick event to call Javascript function
You cannot use OnClick event to call javascript function if you are using ASP: LINKBUTTON OR ASP:BUTTON.

You may use this instead in your ASPX page:

OnClientClick="printPartOfPage('print');" />

DONE.

My Javascript function printPartOfPage() is mainly send the DIV area to printer to print. The content of AreaPrintingService.js :-

// JScript File
function printPartOfPage(elementID)
{
var printContent = document.getElementById(elementID);
var windowURL = "about:blank";
var windowName = "Preview";
var printWindow = window.open(windowURL, windowName, "width=800,height=600,scrollbars=yes");

printWindow.document.write(printContent.innerHTML);
printWindow.document.close();
printWindow.moveTo(300,200);
printWindow.focus();
printWindow.print();

}
-----------------------------------------------------------------------------
Beside using RegisterClientScriptInclude Method, you may consider using RegisterClientScriptBlock Method if your js code is not too long.


String csname = "PopupScript";
Type cstype = this.GetType();

ClientScriptManager cs = Page.ClientScript;

if (!cs.IsStartupScriptRegistered(cstype, csname1))
{
String cstext = "alert('Hello World');";
cs.RegisterStartupScriptBlock(cstype, csname, cstext, true); // last input argument set to TRUE if you do not include tag in your cstext
}

OR If you have Longer script use StringBuilder to build to the script before passing it to RegisStartupScriptBlock

StringBuilder cstext = new StringBuilder();
cstext.Append("text/javascript\"> function DoClick() {");
cstext.Append("Form1.Message.value='Text from client script.'} );
cstext.Append("script>");
cs.RegisterClientScriptBlock(cstype, csname, cstext.ToString(), false); <-- tag already included