Ajax 基础 (asp.net C#)

<script type="text/javascript">
        var xmlHttp;
        function createXMLHttpRequest()
        {
            if(window.ActiveXObject)
            {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            else if(window.XMLHttpRequest)
            {
                xmlHttp = new XMLHttpRequest();
            }
        }
        function startRequest(n)
        {
      var send_string="id="+n;
    send_string= encodeURI(send_string)
            createXMLHttpRequest();
            xmlHttp.onreadystatechange = handleStateChange;
            xmlHttp.open("GET","ReadDate.asp",true);
           // xmlHttp.setRequestHeader("cache-control","no-cache");
            xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            xmlHttp.send(send_string);
        }
        function handleStateChange()
        {
            if(xmlHttp.readyState==4)
            {
                if(xmlHttp.status==200)
                {
                      document.getElementById("Jcon").innerHTML=xmlHttp.responseText

                     //把服务器返回的数据插入ID为Jcon的DIV内
                }
            }
        }
    </script>

调用:οnclick="startRequest(n)"

ASP服务器端处理程序:

<%
Response.Buffer = True
Response.Expires = 0
Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "No-Cache"
response.Charset="GB2312" ' //数据返回的编码类型 显示中文数据必须
Dim id,ahtml
id = Request.Form("id") //获得Ajax客户端发送的ID
set myDb=New dbClass
myDb.sqlStr="select * from Town where Tid = "&id
set rs=myDb.rsDB()
if rs.bof and rs.eof then
Response.Write("<ul style='text-align:center'>没有数据!</ul>")
else
ahtml="<ul><table width='100%' border='0' cellspacing='0' cellpadding='0'>"
ahtml=ahtml+"<tr>"
ahtml=ahtml+"<td class='px14'>&nbsp;&nbsp;&nbsp;"&rs("tit")&"</td>"
ahtml=ahtml+"<td rowspan='2' valign='top'><a href='"&rs("picurl")&"'><img src="&rs("picurl")&" width='145' height='68' /></a></td>"
ahtml=ahtml+"</tr>"       
ahtml=ahtml+"<tr>"
ahtml=ahtml+"<td><li id='TownCon'><a href='about.asp'>"&LeftTrim(rs("content"),230)&"...</a></li></td>"
ahtml=ahtml+"</tr>"
ahtml=ahtml+"</table></ul>"
Response.Write(ahtml) //输出数据
end if
myDb.cRs(rs) '关闭释放rs
set myDb=nothing

%>

ASP.Net服务器端处理程序(ashx):

<%@ WebHandler Language="C#" Class="MenuHandler" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using Souxiu.Data;

public class MenuHandler : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        //不让浏览器缓存
        context.Response.Buffer = true;
        context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";
        context.Response.ClearContent();
        //context.Response.ContentType = "text/plain";
        context.Response.ContentType = "text/html";
        string menuid = context.Request.QueryString["id"];
        context.Response.Write(GetData(menuid).ToString());
        context.Response.End();

    }  

    public bool IsReusable {
        get {
            return false;
        }
    }

    private string GetData(string id)
    {
        string TxtContent = "";

        SqlDataReader dr = null;
        SqlParameter[] param = { Helper.CreateInParam("@parentid", SqlDbType.Int, 4, id), };
        Helper.RunProc("PROC_SystemMenu", out dr, param);

        if (dr.Read())
        {
            while (dr.Read())
            {
                TxtContent += " <ul><li οnclick=/"LeftMenu(1)/" ><a href=/"" + dr["ahref"].ToString() + "/">" + dr["menuName"].ToString() + "</a></li></ul>" + "/r/n";
            }
        }
        else
        {
            TxtContent = "暂无数据!";
        }

        return TxtContent;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值