js无刷新入门问题
Default.aspx.cs代码
public string xx = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string name = this.Request.QueryString["name"];
if (name != null)
{
Response.Write(name);
Response.End();
}
}
}
Default.aspx代码
<!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>user Validation</title>
<script type="text/javascript">
var xmlhttp;
function Validation() {
try {
// Firefox, Opera 8.0+, Safari
xmlhttp = new XMLHttpRequest();
}
catch (e) {
// Internet Explorer
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
alert("您的浏览器不支持AJAX!");
return false;
}
}
}
var name = document.getElementById("Text1");
xmlhttp.open("Post","Default.aspx?name="+name.value,true);
xmlhttp.onreadystatechange = function () {
if(xmlhttp.readyState == 4) {
// alert(xmlhttp.responseText);
document.write(xmlhttp.responseText);
}
}
xmlhttp.send(null);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
aaaa
<input id="Text1" type="text" />
<input id="Button1" type="button" value="button" onclick="Validation()"/>
<%=xx %>
</form>
</body>
</html>
问题是,我按了按妞。xx的内容出来了,可原来的内容没拉。
另外,哪个大大给我介绍个可以学c#.net ajax的网站,多点例子,谢谢。。
------解决方案--------------------
document.write(xmlhttp.responseText);
这句话会重新生成新的文档流 你原来的东西当然没了
改为
document.getElementById('div的id').innerHTML = xmlhttp.responseText;
------解决方案--------------------