62,046
社区成员
发帖
与我相关
我的任务
分享
<script type="text/javascript">
$(document).ready(function(){
$("#div1").ajaxStart(function(){
$("#div1").html("正在加载");
});
$("#btn1").click(function(){
$.post("jQuery.aspx",{key:'btn1',name:$("#txt").val()},function(msg){
$("#div1").html(msg);
});
});
</script>
<input type="button" id="btn1" value="btn1" />
<div id="div1"></div>
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Form["key"] == "btn1")
{
string name = Request.Form["name"];
Response.Clear();
Response.Write(GetClass(name));
Response.End();
}
}
private string GetClass(string name)
{
SqlConnection con = new SqlConnection("server=.;database=stu;uid=sa;pwd=sa");
SqlDataAdapter sda = new SqlDataAdapter("select class from stu where name='" + name + "'", con);
DataSet ds = new DataSet();
sda.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
return ds.Tables[0].Rows[0]["class"].ToString();
}
else
{
return "此人无对应的班级。";
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="AjaxData" %>
<!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">
var xmlHttp;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
else if(window.XMLHttpRequest)
xmlHttp=new XMLHttpRequest();
}
function postTJ(str)
{
createXMLHttpRequest();
xmlHttp.open("post","AjaxData.aspx",true);
xmlHttp.onreadystatechange=stateChange;
xmlHttp.setRequestHeader("content-type","application/x-www-form-urlencoded");
xmlHttp.send("str="+str+"&date="+new Date().getTime());
}
function stateChange()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
alert(xmlHttp.responseText);
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" onclick="postTJ('wujj')" value="Post">
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Request.Form["str"]+"--好好学习");
Response.End();
}
}