Asp.Net+ProtoType框架 绑定DropDownList二级联动 JavaScript函数

// JScript
function CompanyShowTel(companyid,divshow){//Read CompanyShow
//  alert('123');
//  alert(divshow);
  new Ajax.Updater({success:divshow},
  'AjaxFile/CompanyShowTel.aspx?companyid='+companyid+''+'&no-cache='+Math.random(),
  {
  method:'get',parameters:'',onFailure:reportError,evalScripts: true
  }  
  );
//  alert('fff');
  }
  function reportError(request){//Error
//  alert(request);
  alert('Sorry,There was an error');
  }
  
  function CompanyModelShow(companyid,divshow){//Read Mode
//  alert('123');
//  alert(divshow);
  new Ajax.Updater({success:divshow},
  'AjaxFile/CompanyModelShow.aspx?companyid='+companyid+''+'&no-cache='+Math.random(),
  {
  method:'get',parameters:'',onFailure:reportError,evalScripts: true
  }  
  );
//  alert('fff');
  }

function CompanyEmployeeList(companyid,divshow){
    var myAjax = new Ajax.Request('../AjaxFile/Admin/AjaxCompanyEmployee.aspx',{method:'get',parameters:'companyid='+companyid+''+'&no-cache='+Math.random(),onComplete:CompanyEmployeeListShow});   
}

function CompanyEmployeeListShow(Result){
//alert(Result.responseText);
var result=Result.responseText;
//alert(result);
//清空DropDownList
DropDownListClear('ddlClassId');
if(result==null){
return false;
}
//循环添加DropDownList
DropDownListAdd('ddlClassId',result);
}

function DaquCompanyList(companyid,divshow){
    var myAjax = new Ajax.Request('../AjaxFile/Admin/AjaxDaquCompany.aspx',{method:'get',parameters:'companyid='+companyid+''+'&no-cache='+Math.random(),onComplete:DaquCompanyListShow});   
}

function DaquCompanyListShow(Result){
//alert(Result.responseText);
var result=Result.responseText;
//alert(result);
//清空DropDownList
DropDownListClear('ddlCompanyId');
if(result==null){
return false;
}
//循环添加DropDownList
DropDownListAdd('ddlCompanyId',result);
}

          function DropDownListClear(ddlist){//清空DropDownList
          var countnum=document.getElementById(ddlist).options.length;
          for(var i=0;i<=countnum;i++){//清空DropDownList
          document.getElementById(ddlist).remove(countnum-i);
          }
          }
         
          function DropDownListAdd(ddlist,result){//循环添加DropDownList
          var array=result.split("|");
for(var i=0;i<array.length;i++){
var optionarray=array[i].split(",");
var newoption=document.createElement("option");
newoption.text=optionarray[1];
newoption.value=optionarray[0];
document.getElementById(ddlist).options.add(newoption);
}
//var newoption2=document.createElement("option");
//newoption2.text="请选择所属员工";
//newoption2.value="";
//document.getElementById(ddlist).options.add(newoption2);
//alert(document.getElementById(ddlist).options.length);
//alert(document.getElementById(ddlist).options.length-1);
document.getElementById(ddlist).selectedIndex=eval(document.getElementById(ddlist).options.length)-1;
          }
         
    function DropDownListSelected(ddlist,value){
    //alert('value='+value);
    var countnum=document.getElementById(ddlist).options.length;   
//    alert('value2='+value);
    for(var i=0;i<countnum;i++){
    if(document.getElementById(ddlist).options[i].value==value){
//    alert(document.getElementById(ddlist).options[i].value);
    document.getElementById(ddlist).options[i].selected=true;
//    alert('Ok');
    return false;
    }
    }
    }
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET中的DropDownList控件可以通过数据源实现二级联动效果。下面是一个使用C#代码实现的示例: 首先,需要在页面上义两个DropDownList控件,分别表示一级和二级选项: ``` <asp:DropDownList ID="ddlFirstLevel" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlFirstLevel_SelectedIndexChanged"> </asp:DropDownList> <asp:DropDownList ID="ddlSecondLevel" runat="server"> </asp:DropDownList> ``` 在代码后台,我们需要在Page_Load事件中一级选项的数据源,并在一级选项的选中事件中动态二级选项的数据源: ``` protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { // 一级选项的数据源 ddlFirstLevel.DataSource = GetFirstLevelData(); ddlFirstLevel.DataTextField = "Name"; ddlFirstLevel.DataValueField = "ID"; ddlFirstLevel.DataBind(); // 二级选项的初始数据源 ddlSecondLevel.DataSource = GetSecondLevelData(0); // 初始时显示第一个一级选项的二级选项 ddlSecondLevel.DataTextField = "Name"; ddlSecondLevel.DataValueField = "ID"; ddlSecondLevel.DataBind(); } } protected void ddlFirstLevel_SelectedIndexChanged(object sender, EventArgs e) { // 重新二级选项的数据源,根据选中的一级选项 int firstLevelID = Convert.ToInt32(ddlFirstLevel.SelectedValue); ddlSecondLevel.DataSource = GetSecondLevelData(firstLevelID); ddlSecondLevel.DataTextField = "Name"; ddlSecondLevel.DataValueField = "ID"; ddlSecondLevel.DataBind(); } // 获取一级选项的数据源 private DataTable GetFirstLevelData() { // 从数据库或其他途径获取一级选项的数据源 // 返回的DataTable需要包含ID和Name两列,分别表示一级选项的ID和名称 } // 获取二级选项的数据源 private DataTable GetSecondLevelData(int firstLevelID) { // 根据一级选项的ID查询数据库或其他途径获取相应的二级选项数据源 // 返回的DataTable需要包含ID和Name两列,分别表示二级选项的ID和名称 } ``` 以上代码使用了两个DataTable作为示例的数据源,实际使用时需要根据业务需求自行修改数据源的获取逻辑,并将获取到的数据DropDownList控件上。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值