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"/>
<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技术还不是很了解,我希望能与更多已经学习过的人交流,一起进步。。。