Click here to Skip to main content
12,294,475 members (65,394 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

14.4K views
1 bookmarked
Posted

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

, 27 May 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

barry fat
Software Developer (Senior)
Hong Kong Hong Kong
Barry is a Senior Application Developer who lives in HK, worked on a Listed Company, mainly concentrate on microsoft windows server, .net and silverlight technology. He had certified mcts, mcitp, mcpd and Linux+ etc. Email is cshunfat@yahoo.com

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160525.2 | Last Updated 27 May 2012
Article Copyright 2012 by barry fat
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid