用AJAX改变DROPDOWNLIST的值

本例用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 窗体设计器生成的代码
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值