日期:2014-05-17  浏览次数:20557 次

ajax初学问题
HTML code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="Ajax._default" %>

<!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 language="javascript" type="text/javascript">
        var xmlHttp;
        //创建XMLHttpRequest对象
        function createXmlHttpRequest()
        {
            if(window.ActieveXObject) //验证浏览器是否是IE并且支持XMLHttpRequest对象
            {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            else if(window.XMLHttpRequest) //判断浏览器是否支持XMLHttpRequest对象
            {
                xmlHttp=new XMLHttpRequest();
            }
        }
        
        //发起请求
        function startRequest()
        {
            //创建XMLHttpRequest对象
            createXmlHttpRequest();
            //获取页面ID为txt_UserControlName文本框中的内容
            var UserControlName=document.getElementById("txt_UserControlName");
            //写入请求方式
            xmlHttp.open("Post","default.aspx?Control=" + UserControlName.value);
            //请求完毕后,由谁来接收返回的数据
            xmlHttp.onreadystatechange = readyStateChangeHandle;
            //发起请求
            xmlHttp.send(null);
        }
        
        //处理得到的数据
        function readyStateChangeHandle()
        {
            //判断服务器状态
            if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
            {
                //将得到的内容填充给div1
                document.getElementById("div1").innerHTML = xmlHttp.responseText;
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server" method="post">
    <div>
        <input type="text" id="txt_UserControlName" />
        <input type="button" id="btn_UserControlLoad" value="加载..." onclick="startRequest()" />
    </div>
    <div id="div1"></div>
    </form>
</body>
</html>



C# code

using System;

namespace Ajax
{
    public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["Control"] != null && Request["Control"].ToString() != "")
            {
                Response.Write("111111111111");          
            }
        }
    }
}




接收到的HTML,不仅包含11111111,还包含原页面的HTML代码,所以不能放到div1中,怎么解决?

------解决方案--------------------
Response.Write("111111111111");

=》

C# code
  Response.Clear();
            Response.Write("111111");
            Response.End();

------解决方案--------------------
恩,需要包括Clear。

Response.Clear();
Response.Write("111111111111");
Response.Flush();
Response.End();


在aspx上不仅要写这么累赘的代码。