Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Using HTTP Form POST method to pass parameters from a Silverlight application to an ASPX popup window

0.00/5 (No votes)
27 May 2012 1  
How to use HTTP Form POST method to pass parameters from a Silverlight application to an ASPX popup window.

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:

  1. Create an ASPX page called startpage.aspx, which embeds a Silverlight user control called submitform.xaml.
  2. Add a Preview button in submitform.xaml. We click it to send out form data via HTTP POST method.
  3. 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");   //Set hidden field value
    hiddenInput.SetProperty("name", "key1");       //set hidden field name
    hiddenForm.AppendChild(hiddenInput);
    hiddenForm.Invoke("onsubmit");  //submit the form
}

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. Smile | :)

*Remarks: The article is mainly focused on passing a parameter between Silverlight and ASPX so data access for Crystal Report will not be covered.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here