从一个asp.net页面将参数传递给另一个使用jQuery [英] Passing arguments from one asp.net page to another using jQuery

查看:151
本文介绍了从一个asp.net页面将参数传递给另一个使用jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从ASP.NET页面传递4个参数(3串和一个逗号分隔的列表)来使用jQuery另一个ASP.NET页面。目标页面应该被推出作为一个单独的窗口,它正常工作与以下jQuery的片段:

  $('#sourcePageBtn')。点击(函数(){
   window.open(destinationPage.aspx);
   返回false;
});

我如何传递参数到目标页面?我试图避免查询字符串来传递参数,因为:


  1. 我不希望在目标窗口中显示的URL参数(也可以是很长)。


  2. 有像一些特殊字符',/,\\&安培;等在字符串参数。


请建议。

编辑:
我试图访问该aspx文件即的脚本部分的参数。

 < SCRIPT LANGUAGE =C#=服务器>
    保护无效的Page_Load(对象SRC,EventArgs的发送)
    {
     //创建动态ASP控制在这里
    }
< / SCRIPT>

我在脚本部分的Page_Load中的参数特定需要从我创造在Page_Load依赖于这些参数的几个动态图表控件的事实造成的。

欢呼声


解决方案

最初的想法(解决方案中创建前)


  1. 使用大数据而不是GET POST 。随着后没有查询字符串将用于数据,因此URL长度的限制是不是一个问题。 (最大URL长度所以你权当大数据移动到远离它的浏览器之间有所不同)。


  2. 特殊字符的URL可以 EN codeD 在查询字符串中传递,因此不应该是一个问题。


另外,您可以将数据存储在从第一页的服务器端,并具备第二页把它捡起来从服务器端。但是,这是矫枉过正。它使你不需要的服务器编程。

通过HTTP传递状况要求是标准的做法。你不应该试图绕过它。使用它。所有的设施都建在你。现在,它只是到jQuery的为我们提供一些帮助...

注意:注意的使用jQuery在案件的JavaScript在浏览器中禁用主要的应用程序的功能。在大多数情况下,Web应用程序应该是,即使当JavaScript禁用的基层使用。在这之后的工作,于JavaScript / jQuery的层,使体验更加出色,甚至真棒。

修改:解决方案(与ASP.NET处理)

有关解决方案的实施主要资源是:

工作原理:从主页面,后发生,并且结果显示在一个弹出窗口。它发生顺序如下:


  • 主要脚本打开一个弹出窗口(如果它不存在)

  • 主脚本等待弹出窗口完全初始化

  • 主脚本帖子(使用AJAX)的参数到另一个网页(发送请求)

  • 主脚本接收响应,并将其显示在弹出窗口中。

我们实际上已经发布的数据,以一个弹出窗口,并通过参数来处理。

三页跟随,他们构成了完整的解决方案。我把所有3坐在我的桌面上,并在谷歌浏览器的稳定版本3.0.195.38工作。其他浏览器未经测试。您还需要 jQuery的-1.3.2.js 坐在同一个文件夹中。

main_page.html

这是您所提供的逻辑扩展。示例使用一个链接,而不是形式的按钮,但它具有相同的 ID = sourcePageBtn

此示例传递两个键/值对当POST(只是举例)发生。你会通过你选择的键/值对在这个地方。

 < HTML和GT;< HEAD>
<脚本类型=文/ JavaScript的SRC =jQuery的-1.3.2.js>< / SCRIPT>
< /头><身体GT;<一个ID =sourcePageBtn的href =JavaScript的:无效(0);>点击启动弹出窗口和LT; / A><脚本>$(函数(){
 $('#sourcePageBtn')。点击(函数(){  //打开弹出窗口,如果尚未打开,并保存它的句柄到jQuery的数据。
  ($(窗口)。数据('弹出')及与放大器;!$(窗口)。数据('弹出')关闭)
   || $(窗口)。数据('弹出',window.open('popup.html','MyPopupWin'));  //参考弹出的窗口句柄。
  变种wndPop = $(窗口)。数据('弹出');  //等到弹出被加载并准备好了,然后开始使用它
  (waitAndPost =函数(){
   //如果没有加载弹出,等待重试前200毫秒以上
   如果(!wndPop ||!wndPop ['准备'])
    的setTimeout(waitAndPost,200);   其他{
       //逻辑后(通过参数),并在弹出的窗口中显示的结果...
    // POST ARGS名称=约翰,时间=下午到process.aspx网页...
    .post的$('process.aspx',{名字:约翰,时间14:00},功能(数据){
     //并显示在弹出窗口和LT的反应; P>元件。
     $('P',wndPop.document)。html的(数据);
    });
   }
  })(); //首先调用waitAndPost()函数。
 });
});< / SCRIPT>
< /身体GT;
< / HTML>

popup.html

这是从主网页有针对性的弹出窗口。你会看到一个参考的jQuery脚本来popup.html回到主界面。

有一个绝招这里设置窗口['准备'] =真在弹出的窗口中DOM是最后加载。主要的脚本不断检查和等待,直到这个弹出准备。

 < HTML和GT;< HEAD>
<脚本类型=文/ JavaScript的SRC =jQuery的-1.3.2.js>< / SCRIPT>
< /头><身体GT;
 <! - 这个例子P元素里面显示HTTP响应 - >
 < P>页面加载< / P>
< /身体GT;<脚本>
    $(函数(){
     窗口['准备'] =真;
    });
< / SCRIPT>< / HTML>

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

动态服务器页面的参数由主网页脚本发布到。
阿贾克斯参数在 Page.Request 集合。
输出传递回为这个例子纯文本,但你可以自定义您的应用程序要求的响应。

 公共部分类流程:System.Web.UI.Page {    保护无效的Page_Load(对象发件人,EventArgs的发送){
        //访问名的说法。
        字符串则strName =请求[名称]? (无名);
        //访问时间的说法。
        字符串strTime =请求[时间]? (没时间);        Response.ContentType =text / plain的;
        回复于(的String.Format({0}到达{1},则strName,strTime));
    }    保护覆盖无效渲染(System.Web.UI.HtmlTextWriter作家){
        //只是从外部输出HTML标签燮preSS页。
        //base.Render(writer); //不运行。
    }}

这个结果由原始/主页显示为弹出窗口。
所以,在弹出的窗口中的内容将覆盖[名]到达[时间]


主要参考文献: HTTP制造很容易,的 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 arguments (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 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.

这篇关于从一个asp.net页面将参数传递给另一个使用jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆