开发者

Passing arguments from one asp.net page to another using jQuery

I need to pass 4 arguments (3 strings and one comma separated list) from an ASP.NET page to another ASP.NET page using jQuery. The destination page ought to be launched as a separate window, which works fine with the following jQuery snippet:

$('#sourcePageBtn').click(function(){
   window.open("destinationPage.aspx");
   return false;
});

How can I pass the arguments to the destination page? I am trying to avoid the query string to pass the arguments because:

  1. I don't want to show the url argument开发者_StackOverflow中文版s (which can also be very long) in the destination window.

  2. There are some special characters like ',/,\, & etc. in the string arguments.

Please suggest.

Edit: I'm trying to access the arguments in the script section of the aspx file i.e.

<script language="C#" runat="server">
    protected void Page_Load ( object src, EventArgs e) 
    {
     //Creating dynamic asp controls here
    }
</script>

My specific need for the arguments in the Page_Load of the script section stems from the fact that I am creating a few dynamic Chart controls in the Page_Load which depend on these arguments.

cheers


Initial Thoughts (before solution created)

  1. Use POST for large data instead of GET. With POST no querystring will be used for data and therefore URL length restriction isn't a concern. (The max URL length differs between browsers so you're right to stay away from it when large data is moving).

  2. Special URL characters can be encoded to be passed in the query string so that shouldn't be an issue.

Alternatively you might store the data on the server side from the first page, and have the second page pick it up from the server side. But this is overkill. And it makes you do unneeded server programming.

Passing state via HTTP calls is standard practice. You shouldn't try to circumvent it. Work with it. All the facilities are built in for you. Now it's just up to jQuery to provide us some help...

Note: Be careful using jQuery for main app features in case JavaScript is disabled in the browser. In most cases your web application should be usable at a basic level even when JavaScript is disabled. After that's working, layer on JavaScript/jQuery to make the experience even better, even awesome.

Edit: Solution (with ASP.NET processing)

Key resources for solution implementation are:

  • How use POST from jQuery - initiates the request, passes arguments, gets response
  • jQuery context argument - this is how the popup window DOM is accessed/affected from the main window

How it works: From a main page, a POST occurs and results are displayed in a popup window. It happens in this order:

  • The main script opens a popup window (if it doesn't already exist)
  • main script waits for popup window to fully initialize
  • main script POSTs (using AJAX) arguments to another page (sends a request)
  • main script receives response and displays it in the popup window.

Effectively we have posted data to a popup window and passed arguments to the processing.

Three pages follow and they constitute the complete solution. I had all 3 sitting on my desktop and it works in Google Chrome stable version 3.0.195.38. Other browsers untested. You'll also need jquery-1.3.2.js sitting in the same folder.

main_page.html

This is the expansion of the logic you provided. Sample uses a link instead of a form button, but it has the same id=sourcePageBtn.

This sample passes two key/value pairs when the POST occurs (just for example). You will pass key/value pairs of your choice in this place.

<html>

<head>
<script type="text/javascript" src="jquery-1.3.2.js"></script>
</head>

<body>

<a id="sourcePageBtn" href="javascript:void(0);">click to launch popup window</a>

<script>

$(function() {
 $('#sourcePageBtn').click( function() {

  // Open popup window if not already open, and store its handle into jQuery data.
  ($(window).data('popup') && !$(window).data('popup').closed) 
   || $(window).data('popup', window.open('popup.html','MyPopupWin'));

  // Reference the popup window handle.
  var wndPop = $(window).data('popup');

  // Waits until popup is loaded and ready, then starts using it
  (waitAndPost = function() {
   // If popup not loaded, Wait for 200 more milliseconds before retrying
   if (!wndPop || !wndPop['ready']) 
    setTimeout(waitAndPost, 200);

   else  { 
       // Logic to post (pass args) and display result in popup window...
    // POST args name=John, time=2pm to the process.aspx page...
    $.post('process.aspx', { name: "John", time: "2pm" }, function(data) {
     // and display the response in the popup window <P> element.
     $('p',wndPop.document).html(data);
    });
   }
  })(); //First call to the waitAndPost() function.


 });
});

</script>
</body>
</html>

popup.html

This is the popup window that is targeted from the main page. You'll see a reference to popup.html in the jQuery script back in the main page.

There's a "trick" here to set window['ready'] = true when the popup window DOM is finally loaded. The main script keeps checking and waiting until this popup is ready.

<html>

<head>
<script type="text/javascript" src="jquery-1.3.2.js"></script>
</head>

<body>
 <!--  The example P element to display HTTP response inside -->
 <p>page is loaded</p>
</body>

<script>
    $(function() {
     window['ready'] = true;
    });
</script>

</html>

process.aspx.cs (C# - ASP.NET process.aspx page)

The dynamic server page the arguments are POSTed to by the main page script. The AJAX arguments arrive in the Page.Request collection. The output is delivered back as plain text for this example, but you can customize the response for your apps requirements.

public partial class process : System.Web.UI.Page {

    protected void Page_Load(object sender, EventArgs e) {
        // Access "name" argument.
        string strName = Request["name"] ?? "(no name)";
        // Access "time" argument. 
        string strTime = Request["time"] ?? "(no time)";

        Response.ContentType = "text/plain";
        Response.Write(string.Format("{0} arrives at {1}", strName, strTime));
    }

    protected override void Render(System.Web.UI.HtmlTextWriter writer) {
        // Just to suppress Page from outputting extraneous HTML tags.
        //base.Render(writer); //don't run.
    }

}

Results of this are displayed into the popup window by the original/main page. So the contents of the popup window are overwritten with "[name] arrives at [time]"


Main References: HTTP Made Really Easy, jQuery Ajax members and examples.


If you keep a reference to the new window when you open it, ie var destWin = window.open(...) then you can access the variables and methods on the destWin window object. Alternatively you can "reach back" from the destination window with window.opener.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜