用AJAX改变DROPDOWNLIST的值

来源:互联网 发布:淘宝默认好评计分吗 编辑:程序博客网 时间:2024/05/16 00:54

本例用2003写的,当text中的值改变时,而 改变DORPDOWNLIST中的值,实现代码如下:

index.asp

<%@ Page language="c#" Codebehind="Index.aspx.cs" AutoEventWireup="false" Inherits="Demo.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    
<HEAD>
        
<title>WebForm1</title>
        
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        
<meta name="CODE_LANGUAGE" Content="C#">
        
<meta name="vs_defaultClientScript" content="JavaScript">
        
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
        
<script>
         
var httpRequest;
        
function TextChanged()
        
{
         
var obj=document.getElementById("txtInput");
         
var pra="2";
          createXMLHttpRequest();
             httpRequest.onreadystatechange 
= doContents;//调用doContents函数 
            httpRequest.open('GET'"request.aspx?pra="+pra, true);//根据不同的PRA值来调用不同的服务端方法;
            httpRequest.send(null); 
        }

        
        
function doContents()
        
{
        
         
if(httpRequest.readystate==4)
         
{
          
if (httpRequest.status==200)
          
{
            
var str=httpRequest.responseText;
            AddDropDownListItem(str);
          }

         }

         
         
function AddDropDownListItem(str)
         
{
            
var objItem=str.split('@');
            
var obj=document.getElementById("ddlSelectList");
            obj.options.length
=0;
            
var i;
            
for(i=0;i<objItem.length;i++)
            
{
                
var item=objItem[i].split(';');
                
if(item[0]!=""&&item[1]!="")
                
{
                obj.options.add(
new Option(item[0],item[1]));
                }

                
            }

         }

        
        }

        
        
function CreateXmlRequestHttp()
        
{
        
if(window.XMLHttpRequest)// 在非IE浏览器中创建XMLHttpRequest对象
        {
            httpRequest
=new XMLHttpRequest();
        }

        
else if(window.ActiveXObject)
        
{
            httpRequest
=new  ActiveXObject("Microsoft.XMLHTTP");//通过MS ActiveX创建XMLHttpRequest<BR>
        }

        
else
        
{
            alert(
"你的浏览器不能使用AJAX运用!");
        }

        
        }

        
        
function createXMLHttpRequest() 
        
{
  
  
if (window.XMLHttpRequest) 
  
// 在非IE浏览器中创建XMLHttpRequest对象
    httpRequest = new XMLHttpRequest();
  }
 else if (window.ActiveXObject) //通过MS ActiveX创建XMLHttpRequest
    try {
      
// 尝试按新版InternetExplorer方法创建
      httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    }
 catch (e1) // 创建请求的ActiveX对象失败
      try {
        
// 尝试按老版InternetExplorer方法创建
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }
 catch (e2) {
        
// 不能通过ActiveX创建XMLHttpRequest
      }

    }

  }

 
}
 
        
        
        
        
</script>
    
</HEAD>
    
<body MS_POSITIONING="GridLayout">
        
<form id="Form1" method="post" runat="server">
            
<input type="text" name="txtInput" id="txtInput" onblur="TextChanged()">
            
<asp:DropDownList id="ddlSelectList" style="Z-INDEX: 101; LEFT: 192px; POSITION: absolute; TOP: 16px"
                runat
="server"></asp:DropDownList>
        
</form>
    
</body>
</HTML>

request.cs 用来处理回调事件!

 

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Demo
{
    
/// <summary>
    
/// request 的摘要说明。
    
/// </summary>

    public class request : System.Web.UI.Page
    
{
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
            Response.Clear();
            
string pra=Request.QueryString["pra"].ToString();
            
            
if(pra=="1")
            
{
                Response.Write(GetString1());
                }

            
else
            
{
Response.Write(GetString2());
            }

            Response.End();
        }


        
private string GetString1()
        
{
            
return "First;1@Second:2@";
        }


        
private string GetString2()
        
{
            
return "Third;1@Ten;2@";
        }

        
Web 窗体设计器生成的代码
    }

}

原创粉丝点击