I'm passing Ajax arguments from a source ASP.NET page to destination ASP.NET page. The code snippet looks like the following:
$('#sourcePageBtn').click( function() {
$.post('destination.aspx', { name: "John", time: "2pm" }, function(data) {
});
});
I'm trying to access the arguments in the script section of the destination 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.
Problem: - I don开发者_高级运维't see the arguments in the destination file. I tried to fetch them using Request["name"]
and Request["time"]
.
Please let me know where it has gone wrong.
P.S. - I had this SO post which dealt with launching a new page from the jQuery section of source page and at the end all worked fine except for this argument capture.
cheers
Instead of sending it as a post request send as a get request and try Request.Params[0] and Request.Params[1]
I just created a new async httphandler and ajax call is like:
$("#btnAjaxLoad").click(function() {
$.ajax({ type: "GET",
data: ({name : 'John', time : '8pm'}),
url: "DataSourceAsync.ashx",
contentType: "text/html; charset=utf-8",
dataType: "html",
success: function(data) { $("#AJAXGenerated").show(), $("#AJAXGenerated").html(data); $("#loading").hide(); }
});
});
Now I am able to access both Jhon and 8pm in the beginProcess event as context.Request.Params[0] and context.Request.Params[1] but as soon I change the type: GET to POST both the parameters are not there.
It is hard to say without seeing all your code but it could be 1 of 2 problems(or both).
First what kind of button is it? Submit button? asp.net button(what is a submit button) or a plan button?
If they are submit buttons then that is your problem right there as that will do a full post back and your ajax request won't happen.
The second case could be that from your jquery code you don't bind the click function on load so that function never gets binded so when you click on a button it won't do anything.
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
</script>
<script type="text/javascript">
$(function()
{
$('#sourcePageBtn').click(function()
{
$.post('Default.aspx', { name: "John", time: "2pm" }, function(data)
{
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="sourcePageBtn" type="button" value="button" />
<%--asp button won't work as it does full post back --%>
<asp:Button ID="sourcePageBtn" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string test = Request["name"];
}
}
Also will have the same results if you put it as script tag.
<script language="C#" runat="server">
protected void Page_Load ( object src, EventArgs e)
{
//Creating dynamic asp controls here
string test = Request["name"];
}
</script>
精彩评论