Ajax实现两级下拉框连动的应用

<asp:DropDownList onChange="GetChange(this);" id="drp1" runat="server"></asp:DropDownList>
   <asp:DropDownList id="drp2" runat="server"></asp:DropDownList>

js脚本:
ContractedBlock.gif ExpandedBlockStart.gif 返回数据
None.gif            function GetChange(drp)
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif{
InBlock.gif                
var num = drp.options[drp.selectedIndex].value;
InBlock.gif                WebForm3.GetDrp(num,drp_CallBack);
ExpandedBlockEnd.gif            }

None.gif            
function drp_CallBack(response)
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif{
InBlock.gif                
if(response.error != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    alert(response.error);
InBlock.gif                    
return;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
var drp = document.getElementById("<% = drp2.ClientID %>");
InBlock.gif                
var data = response.value;
InBlock.gif                
if(data == null || typeof(data) != 'object')
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return;
ExpandedSubBlockEnd.gif                }

InBlock.gif                drp.options.length 
= 0;
InBlock.gif                
for(i=0;i<data.length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
var o = data[i];
InBlock.gif                    drp.options[drp.options.length] 
= new Option(data[i],data[i]);
ExpandedSubBlockEnd.gif                }

ExpandedBlockEnd.gif            }

后台代码实现
ContractedBlock.gif ExpandedBlockStart.gif
None.gif        private void Page_Load(object sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
// 在此处放置用户代码以初始化页面
InBlock.gif
            Ajax.Utility.RegisterTypeForAjax(typeof(ZKMarketing.WebForm3));
InBlock.gif            
if(!Page.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for(int i=5;i<11;i++)
InBlock.gif                    drp1.Items.Add(
new ListItem(i.ToString(),i.ToString()));
InBlock.gif                drp2.Items.Add(
new ListItem("请选择","-1"));
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif        [Ajax.AjaxMethod()]
None.gif        
public ArrayList GetDrp(int num)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            ArrayList al 
= new ArrayList(num);
InBlock.gif            al.Clear();
InBlock.gif            
for(int i=0;i<num;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ListItem li 
= new ListItem(i.ToString(),i.ToString());
InBlock.gif                al.Add(li);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return al;
ExpandedBlockEnd.gif        }
同时要设置web.Config文件,  
<httpHandlers> 
     <add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
  </httpHandlers>

上面是我在做一个具体项目的时候,做的一个测试页面,由于是第一次,所以就写了上面的这样一个简单的测试页面来实现AJAX,现在具体的项目中的功能也基本上完成。

转载于:https://www.cnblogs.com/Oceanchip/archive/2005/09/26/244595.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值