现在的位置: 首页 > 综合 > 正文

Ajax异步请求asp.net页面

2012年11月10日 ⁄ 综合 ⁄ 共 8211字 ⁄ 字号 评论关闭

Ajax代码:

<script language="javascript" type="text/javascript">
        
var xmlObj;
        
//创建XMLHttpRequest对象       
        function createXMLHttpRequest() 
        {
            
if(window.XMLHttpRequest) 
            { 
//Mozilla 浏览器
                xmlObj = new XMLHttpRequest();
            }
            
else if (window.ActiveXObject) 
            { 
// IE浏览器
                try 
                {
                    xmlObj 
= new ActiveXObject("Msxml2.XMLHTTP");
                } 
                
catch (e) 
                {
                    
try 
                    {
                        xmlObj 
= new ActiveXObject("Microsoft.XMLHTTP");
                    } 
                    
catch (e) {}
                }
            }
        }
        
function load() 
        {
            createXMLHttpRequest();
            
var customerid=document.getElementById("ajaxCustomerid").value;
            
var vouchid=document.getElementById("ajaxVouchid").value
            
var url="Querycustomerinfodetail_Ajax.aspx?customerid="+escape(customerid)+"&vouchid="+escape(vouchid)
            xmlObj.open(
"POST",url,true);
            xmlObj.setRequestHeader(
"Content-Type""application/x-www-form-urlencoded");//如果是post协议,则该参数必不可少
            xmlObj.onreadystatechange = handleResponse;            
            xmlObj.send(
null); 
        }
        
function handleResponse() 
        {
            
if (xmlObj.readyState == 4)
            {
                
if (xmlObj.status == 200)
                {
                    
//document.getElementById("lblCustomerCount").innerText=xmlObj.responseText.getElementById("hf")
                    document.getElementById("ajaxData").innerHTML=unescape(xmlObj.responseText);
                }
                
else
                {
                    window.alert(
"请求页面发生异常!");
                }
            }
        }
        
//setInterval('load()',5000);//设置更新时间
        </script>

 

 

<span id="ajaxData"><img src="../images/large-loading.gif" width="32" height="32" style="margin-right:8px;" />正在加载客户层次</span>

 

.aspx页面代码:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Querycustomerinfodetail_Ajax.aspx.cs" Inherits="CustomerService_Querycustomerinfodetail_Ajax" %>

 

.aspx.cs代码:

 

using System;
using System.Configuration;
using System.Collections;
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;
using Microsoft.Web.UI.WebControls;
using Wapdm.AirTicket.Data.CustomerService.Data;
using System.Data;
using Wapdm.AirTicket.DataAccess;
using Wapdm.AirTicket.Business.CustomerService;
using System.Text;

public partial class CustomerService_Querycustomerinfodetail_Ajax : System.Web.UI.Page
{
    
private string customerid="";
    
private string vouchid="";
    
protected void Page_Load(object sender, EventArgs e)
    {
        
if (!Page.IsPostBack)
        {
            customerid 
= Request.QueryString["customerid"];
            vouchid 
= Request.QueryString["vouchid"];
            GetCommend();
        }
    }
    
private void GetCommend()
    {
        Microsoft.Web.UI.WebControls.TreeView TreeView1 
= new Microsoft.Web.UI.WebControls.TreeView();
        TreeView1.ExpandLevel 
= 2;
        TreeView1.DefaultStyle 
= CssCollection.FromString("color:#0066cc;");
        
int iCustomerCount = 0;
        Microsoft.Web.UI.WebControls.TreeNode tn 
= new Microsoft.Web.UI.WebControls.TreeNode();
        
if (vouchid != "")
        {
            tn.Text 
= vouchid;
            tn.NavigateUrl 
= "QueryCustomerInfoDetail.aspx?CustomerId=" + vouchid;
        }
        
else
        {
            tn.Text 
= "";
        }
        TreeView1.Nodes.Add(tn);

        Microsoft.Web.UI.WebControls.TreeNode tnc = new Microsoft.Web.UI.WebControls.TreeNode();
        tnc.Text 
= "其他";
        tn.Nodes.Add(tnc);

        tnc = new Microsoft.Web.UI.WebControls.TreeNode();
        tnc.Text 
= "<font color=red><b>" + customerid + "</b></font>";
        tn.Nodes.Add(tnc);

        DataBaseOperate dbo = new DataBaseOperate();
        CustomerInfoSystem customerInfoSystem 
= new CustomerInfoSystem(dbo);
        DataSet ds 
= customerInfoSystem.GetCustomerInfo(
            
-1,
            
-1,
            
-1,
            
"",
            
"",
            
"",
            
"",
            
"",
            
"",
            Convert.ToInt32(customerid),
            
-1,
            
-1);
        
for (int i = 0; i < ds.Tables[0].Rows.Count - 1000; i++)
        {
            Microsoft.Web.UI.WebControls.TreeNode tnc1 
= new Microsoft.Web.UI.WebControls.TreeNode();
            
if (ds.Tables[0].Rows[i]["Mobile"].ToString().Trim() != "")
            {
                tnc1.Text 
= ds.Tables[0].Rows[i]["CustomerId"].ToString() + "(" + ds.Tables[0].Rows[i]["Mobile"].ToString().Trim() + ")";
            }
            
else
            {
                tnc1.Text 
= ds.Tables[0].Rows[i]["CustomerId"].ToString() + "(" + ds.Tables[0].Rows[i]["Email"].ToString().Trim() + ")";
            }
            
if (ds.Tables[0].Rows[i]["CustomerName"].ToString() != "")
            {
                tnc1.Text 
+= ds.Tables[0].Rows[i]["CustomerName"].ToString();
            }

            tnc1.NavigateUrl = "QueryCustomerInfoDetail.aspx?CustomerId=" + ds.Tables[0].Rows[i]["CustomerId"].ToString();
            tnc.Nodes.Add(tnc1);

            ++iCustomerCount;

            //第二层结构
            DataSet ds1 = customerInfoSystem.GetCustomerInfo(
                
-1,
                
-1,
                
-1,
                
"",
                
"",
                
"",
                
"",
                
"",
                
"",
                Convert.ToInt32(ds.Tables[
0].Rows[i]["CustomerId"]),
                
-1,
                
-1);
            
for (int j = 0; j < ds1.Tables[0].Rows.Count; j++)
            {
                Microsoft.Web.UI.WebControls.TreeNode tnc2 
= new Microsoft.Web.UI.WebControls.TreeNode();
                
if (ds1.Tables[0].Rows[j]["Mobile"].ToString().Trim() != "")
                {
                    tnc2.Text 
= ds1.Tables[0].Rows[j]["CustomerId"].ToString() + "(" + ds1.Tables[0].Rows[j]["Mobile"].ToString().Trim() + ")";
                }
                
else
                {
                    tnc2.Text 
= ds1.Tables[0].Rows[j]["CustomerId"].ToString() + "(" + ds1.Tables[0].Rows[j]["Email"].ToString().Trim() + ")";
                }
                
if (ds1.Tables[0].Rows[j]["CustomerName"].ToString().Trim() != "")
                {
                    tnc2.Text 
+= ds1.Tables[0].Rows[j]["CustomerName"].ToString();
                }

                tnc2.NavigateUrl = "QueryCustomerInfoDetail.aspx?CustomerId=" + ds1.Tables[0].Rows[j]["CustomerId"].ToString();
                tnc1.Nodes.Add(tnc2);
                
++iCustomerCount;
            }
        }
        
//txtBaseCustomeridCount.Value = iCustomerCount.ToString();
        
//HiddenField hf = new HiddenField();
        
//hf.Value = iCustomerCount.ToString();
        dbo.CloseDb();
        StringBuilder sbString
=new StringBuilder();
        sbString.Append(
"客户层次:(共"+iCustomerCount.ToString());
        sbString.Append(
"名)");
        sbString.Append(
"<br>");
        sbString.Append(
"<table style='BORDER-RIGHT: silver 1px solid; BORDER-TOP: silver 1px solid; BORDER-LEFT: silver 1px solid; BORDER-BOTTOM: silver 1px solid' cellpadding='0' cellspacing='0' border='0' bgcolor='white'>");
        sbString.Append(
"<tr>");
        sbString.Append(
"<td>");
        sbString.Append(
"<div style='OVERFLOW: auto; WIDTH: 296px; HEIGHT: 456px'>");
        sbString.Append(RenderHTML(TreeView1));
        sbString.Append(
" </div>");
        sbString.Append(
"</td>");
        sbString.Append(
"</tr>");
        sbString.Append(
"</table>");               
        Response.Write(sbString.ToString());
        
//Response.Write(RenderHTML(hf));
    }

    /// <summary>
    
/// 服务器端提取控剑生成的HTML
    
/// </summary>
    
/// <param name="objWebCtrl"></param>
    
/// <returns></returns>
    public string RenderHTML(WebControl objWebCtrl)
    {
        
try
        {
            System.IO.StringWriter sw 
= new System.IO.StringWriter();
            HtmlTextWriter html 
= new HtmlTextWriter(sw);

            objWebCtrl.RenderControl(html);
            return sw.ToString();
        }
        
catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            
return "";
        }
    }
}

抱歉!评论已关闭.