如何在asp.net web表单使用jquery ajax的发布和访问数据? [英] How to post and access the data using jquery ajax in asp.net webforms?
问题描述
下面是我目前的code:
下面是Default.aspx的
<身体GT;
<表ID =form1的=服务器>
< DIV ID =mydiv> < / DIV>
< /表及GT;
<脚本>
$(文件)。就绪(函数(){ $阿贾克斯({
网址:'Default2.aspx',
数据:{'名':'+兰德尔+'}, 键入:POST,
成功:函数(){ //警报('进行插入。');
$(#mydiv),空()。 $(#mydiv)负载(Default2.aspx #div); },
错误:功能(数据,状态,jqXHR){警报(jqXHR); }
}); })
< / SCRIPT>
< /身体GT;
那么对于Default2.aspx,我想访问这样的数据:
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
。Label1.Text =的Request.Form [名称]的ToString(); }
这看起来像你想使用的WebMethod 在ASP.NET:
$(文件)。就绪(函数(){
$阿贾克斯({
网址:'Default2.aspx /的HelloWorld,
数据:{'名':'+兰德尔+'},
键入:POST,
的contentType:应用/ JSON的;字符集= UTF-8,
数据类型:JSON
成功:功能(数据){
//警报('进行插入。');
$(#mydiv),空()。
$(#mydiv)的html(数据);
},
错误:功能(数据,状态,jqXHR){警报(jqXHR); }
});
});
和在你的code后面,你应该这样做:
[的WebMethod()]
公共静态字符串的HelloWorld(字符串名称)
{
字符串消息=你好+名称;
返回消息;
}
的WebMethods在某种比做更好的 __ doPostBack()因为你CONTROLL例如使用jQuery的所有客户端 - 服务器的流量。
更多的WebMethods:的这里或只是谷歌的WebMethods ASP.NET。
如果你想要接受某种形式的价值,你应该把它放在 $。ajax的数据参数和添加相同参数的的WebMethod
EDITED
从code您发表我看到你想从Default.aspx的一些数据Default2.aspx发送到Default2.aspx并加载一些内容(#div)。
您可以这样做:
$。阿贾克斯({
网址:/Default2.aspx
键入:GET,
数据类型:HTML,
异步:假的,
数据:{名:兰德尔
},
成功:函数(OBJ){
// OBJ将包含请求页面的完整内容
//使用jQuery只提取body标签内的HTML
$内容= $(OBJ).find('身体#div)HTML()。
//然后用此更新对话内容,并显示它
}
});
和在code背后:
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
Label1.Text =的Request.QueryString [名称];
}
Here is my current code:
Here is for default.aspx
<body>
<form id="form1" runat="server">
<div id="mydiv">
</div>
</form>
<script>
$(document).ready(function () {
$.ajax({
url: 'Default2.aspx',
data: "{ 'name': '" + "randel" + "' }",
type: "POST",
success: function () {
// alert('insert was performed.');
$("#mydiv").empty();
$("#mydiv").load("Default2.aspx #div");
},
error: function (data, status, jqXHR) { alert(jqXHR); }
});
})
</script>
</body>
Then for Default2.aspx, I want to access the data like this:
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Request.Form["name"].ToString();
}
This looks like you want to use WebMethod in ASP.NET:
$(document).ready(function () {
$.ajax({
url: 'Default2.aspx/HelloWorld',
data: "{ 'name': '" + "randel" + "' }",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
// alert('insert was performed.');
$("#mydiv").empty();
$("#mydiv").html(data);
},
error: function (data, status, jqXHR) { alert(jqXHR); }
});
});
and in your code behind you should do this:
[WebMethod()]
public static string HelloWorld(string name)
{
string message = "Hello " + name;
return message;
}
WebMethods are in some kind better than doing __doPostBack() because you controll all client-server traffic using for example jQuery. More about WebMethods: here or just google WebMethods ASP.NET.
And If you want to receive some form value you should put it on $.ajax data parameter and add the same parameter in WebMethod.
EDITED
From the code you post I see that you want send from Default.aspx some data to Default2.aspx and load some content from Default2.aspx (#div).
You can do this:
$.ajax({
url: "/Default2.aspx",
type: "GET",
dataType: "html",
async: false,
data: { "name": "randel"
},
success: function (obj) {
// obj will contain the complete contents of the page requested
// use jquery to extract just the html inside the body tag
$content = $(obj).find('body #div').html();
// then update the dialog contents with this and show it
}
});
And in code behind:
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Request.QueryString["name"];
}
这篇关于如何在asp.net web表单使用jquery ajax的发布和访问数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!