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

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

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

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

复制代码
<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>
复制代码

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

复制代码
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");
}
}
复制代码

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

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

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

转载于:https://www.cnblogs.com/cugwx/p/3566435.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值