首页 > Web开发 > 详细

Asp.net--Ajax前后台数据交互

时间:2014-02-25 22:40:45      阅读:465      评论:0      收藏:0      [点我收藏+]

转自:http://www.cnblogs.com/guolebin7/archive/2011/02/22/1961737.html

代码由前后台两部分组成:

前台:(新建一个Default.aspx)

bubuko.com,布布扣
bubuko.com,布布扣
<head runat="server">
<title>Ajax,I am coming</title>

<style type = "text/css">
*
{ font-family:Tahoma, Arial, Sans-Serif;}
#helloTitle
{ color:#48f; font-size:1.5em;}
</style>

<script type = "text/javascript" src = "prototype.js">/*需加入prototype类库*/
</script>

<script type = "text/javascript">
window.onload
=function() {
document.getElementById(
helloBtn)
.onclick
=function() {
/*为helloBtn按钮编写一个方法*/
var name = document.getElementById(helloTxt).value;
new Ajax.Request(
"Ajax.aspx?name="+ encodeURI(name), /*新建一个Ajax对象,数据传到Ajax.aspx进行处理*/
{
method:
"get",
onComplete:
function(xhr) {
document.getElementById(
helloTitle)
.innerHTML
= xhr.responseText;
}
}
)
}
}
</script>
</head>

<body>
<form id="form1" runat="server">
<div>
<h1 id = ‘helloTitle‘>hello,stranger</h1>
<p>Please introduce yourself by entering your name in the box below</p>
<input type = "text" size = "24" id = "helloTxt"/>
&nbsp;
<input type = "button" id = "helloBtn" value = "Submit"/>
</div>
</form>
</body>
</html>
bubuko.com,布布扣
bubuko.com,布布扣

后台:(新建一个Ajax.aspx页面,在Ajax.aspx.cs中加入下面的代码)

bubuko.com,布布扣
bubuko.com,布布扣
protectedvoid Page_Load(object sender, EventArgs e)
{
string a = Request.QueryString["name"]; /*取出name变量的值,个人觉得与Asp.net取Session值类似*/
SqlConnection conn
=new SqlConnection("Data Source=X0O4MQIY5N0SXOY;Initial Catalog=kk;Integrated Security=True");
string sql ="select t_id from tablename where t_name = ‘"+ a +"";
SqlCommand cmd
=new SqlCommand(sql,conn);
SqlDataAdapter myda
=new SqlDataAdapter(cmd);
DataSet myDs
=new DataSet();
myda.Fill(myDs);

if (myDs.Tables[0].Rows.Count >0)
{
Response.Write(
"true");
}
else
{
Response.Write(
"false");
}
}
bubuko.com,布布扣
bubuko.com,布布扣

在ASP.net中利用Ajax技术、表单进行一个前后台的数据交互。

一个小小的实例,还得继续学习。

请各位看到这篇博客的人如果有自己的想法,请留下的您宝贵的想法,我现在对在ASP.net中利用Ajax技术还不是很了解,我希望能与更多已经学习过的人交流,一起进步。。。

Asp.net--Ajax前后台数据交互

原文:http://www.cnblogs.com/cugwx/p/3566435.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!