Introduction
I recently developed a Silverlight application and I wanted to use Crystal Reports in the Silverlight application.
In fact, I discovered it is impossible to use a Silverlight user control to embed a Crystal Reports Viewer. So I changed my design to use
another method that allows to pass arguments from a Silverlight user control to a popup ASPX page which has a Crystal Reports
Viewer inside of it. Also, I use an HTTP form submit to implement it. As we know, the form methods can be POST
and GET. In the following demo, I prefer to use the form POST method because using a querystring limits the parameters
length but the POST method has no limits.
Technique
To begin with, we should create a Silverlight application in Visual Studio. The demonstration files include a start page called startpage.aspx, a submit form user control called submitform.xaml inside startpage.aspx, and another page called reportviewer.aspx, which includes the Crystal Reports Viewer.
The whole workflow is as below:
- Create an ASPX page called startpage.aspx, which embeds a Silverlight user control called submitform.xaml.
- Add a Preview button in submitform.xaml. We click it to send out form data via HTTP POST method.
- Based on form data, pop up the Crystal Reports result in reportviewer.aspx.
Here is some code required to do this:
Firstly, we need to add an empty form, whose id is "postform
" in startpage.aspx and JavaScript to pop up the form.
<script type="text/javascript">
function openNewSpecifiedWindow(thisform) {
window.open(thisform.action.toString(), thisform.target.toString(),
'width=700,height=400,location=no,scrollbars=no,status=no,resizable=no,menubar=no,toolbar=no');
thisform.submit();
}
</script>
<form id="postform" onsubmit="openNewSpecifiedWindow(this);" />
Afterwards, we need to add a Preview button in submitform.xaml:
<Button Content="Preview" Height="23"
HorizontalAlignment="Left" Margin="146,200,0,0"
Name="btnPreview" VerticalAlignment="Top"
Width="75" Click="btnPreview_Click" />
The code behind file is submitform.cs. The HtmlElement
class is implemented in the following code,
which sets a hidden field called key1
and will be sent out through the HTTP POST method. The hidden field value is "data1
".
private void btnPreview_Click(object sender, RoutedEventArgs e)
{
HtmlElement hiddenForm = HtmlPage.Document.GetElementById("postform");
HtmlElement hiddenInput = HtmlPage.Document.CreateElement("input");
hiddenForm.SetProperty("action", "./reportviewer.aspx");
hiddenForm.SetProperty("method", "post");
hiddenForm.SetProperty("target", "reportingPage");
hiddenInput.SetProperty("type", "hidden");
hiddenInput.SetProperty("value", "data1"); hiddenInput.SetProperty("name", "key1"); hiddenForm.AppendChild(hiddenInput);
hiddenForm.Invoke("onsubmit"); }
In reportviewer.aspx, we add the code to embed the Crystal Report Viewer.
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"
AutoDataBind="True" Height="799px"
ReportSourceID="CR1" Width="500px" />
<CR:CrystalReportSource ID="CR1" runat="server">
<Report FileName="ReportTemplate.rpt">
</Report>
</CR:CrystalReportSource>
At last, we can use the httpcontext
object to access the hidden field. The code is added to the code behind file reportviewer.cs.
string str = HttpContext.Current.Request.Form["key1"];
Follow the steps, and you feel it is easy.
*Remarks: The article is mainly focused on passing a parameter between Silverlight and ASPX so data access for Crystal Report will not be covered.