用JS实现dropdownList的选择改变时不重新加载页面就改变二级菜单的值

后台代码:
将第一级的dropdownList里面所需的数据在类中声名为public static string,将每一个一级菜单所对应的二级菜单用分号“,”连接起来。在获取用户所选值之前必须有 protected override void Render这个函数。

 public partial class EMRDiagnostic : System.Web.UI.Page
    {
        public static string adjust = "";
        public static string reboot = "";
        public static string reburn = "";
        public static string reinsert = "";
        public static string replace = "";
        public static string rescan = "";
        public static string AddorRemove = "";
        public static string NFF = "";
        public static string rework = "";
          public void GetRepairCategory()
        {
            DB_IDC.IDCDB_EMR_DIAG_DataLoad dd = new DB_IDC.IDCDB_EMR_DIAG_DataLoad();
            DataTable dt = dd.GetRepairCategoryWholeTable();
            adjust = "";         //static参数,每次和数据库交互就 必须清空之前内容!!
            reboot = "";
            reburn = "";
            reinsert = "";
            replace = "";
            rescan = "";
            AddorRemove = "";
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["RepairCategory"].ToString() == "adjust") { adjust = adjust + dr["RepairCategory2Sub"].ToString() + ","; }
                if (dr["RepairCategory"].ToString() == "reboot") { reboot = ""; }
                if (dr["RepairCategory"].ToString() == "reburn") { reburn = ""; }
                if (dr["RepairCategory"].ToString() == "reinsert") { reinsert = reinsert + dr["RepairCategory2Sub"].ToString() + ","; }
                if (dr["RepairCategory"].ToString() == "replace") { replace = replace + dr["RepairCategory2Sub"].ToString() + ","; }
                if (dr["RepairCategory"].ToString() == "rescan") { rescan = rescan + dr["RepairCategory2Sub"].ToString() + ","; }
                if (dr["RepairCategory"].ToString() == "Add or Remove") { AddorRemove = AddorRemove + dr["RepairCategory2Sub"].ToString() + ","; }
            }
            adjust = adjust.Substring(0, adjust.Length - 1);
            reinsert = reinsert.Substring(0, reinsert.Length - 1);
            replace = replace.Substring(0, replace.Length - 1);
            rescan = rescan.Substring(0, rescan.Length - 1);
            AddorRemove = AddorRemove.Substring(0, AddorRemove.Length - 1);
        }
          protected override void Render(HtmlTextWriter writer)       //  为了获取前台被JS修改过的DropDownList的值
        {
            ClientScript.RegisterForEventValidation(this.ddlShow.UniqueID, "argument");//ddlShow为DropDownList控件的名称
            ClientScript.RegisterForEventValidation(this.ddlCla.UniqueID, "argument");//ddlCla为DropDownList控件的名称
            base.Render(writer);
        }
         protected void Page_Load(object sender, EventArgs e)
        {
         if (!IsPostBack)
            {
             this.GetRepairCategory();//读取维修类型的分类,赋值给static参数们,adjust,reboot,reburn,reburn,reinsert,replace,rescan,AddorRemove
}
        
}
  protected void ImgBtnEmrDiagSubmit_Click(object sender, ImageClickEventArgs e)
        {
  //RepairDropDownList获取到的被JS修改过的值
            string RepairComm = Request.Form[this.ddlCla.UniqueID.ToString()] + "," + Request.Form[this.ddlShow.UniqueID.ToString()];  
}
}

前台代码:

<asp:DropDownList ID="ddlCla" runat="server" onChange="test();"  Width="160px" Font-Names="Calibri" Height="23px">
                                                  <asp:ListItem>Add or Remove</asp:ListItem>
                                                  <asp:ListItem>adjust</asp:ListItem>
                                                  <asp:ListItem>reboot</asp:ListItem>
                                                  <asp:ListItem>reburn</asp:ListItem>
                                                  <asp:ListItem>reinsert</asp:ListItem>
                                                  <asp:ListItem>replace</asp:ListItem>
                                                  <asp:ListItem>rescan</asp:ListItem>
                                                 <asp:ListItem>NFF</asp:ListItem>
                                                  <asp:ListItem>rework</asp:ListItem>
                                                  </asp:DropDownList>
                                                 <asp:DropDownList ID="ddlShow" runat="server" Width="160px" AppendDataBoundItems="True" Font-Names="Calibri" Height="23px">
                                                    </asp:DropDownList>

前台JS代码:

 var x = [9];
            x[0] ='<%=AddorRemove%>';  
            x[1] = '<%=adjust%>';
            x[2] =  '<%=reboot%>';
            x[3] = '<%=reburn%>';
            x[4] = '<%=reinsert%>';
            x[5] = '<%=replace%>';
            x[6] = '<%=rescan%>';  
            x[7] = '<%=NFF%>';
            x[8] = '<%=rework%>';  
           function test() {
              var num, x1, i;
              num = $("ddlCla").selectedIndex;  //获取选中大学科类别的索引值     
              if (x[num].toString() == "")
                 x1 = "   ";
              else {
                  x1 = x[num].split(",");  //获取相应的小学科列表
              }
              var downlist = $("ddlShow");
              downlist.options.length = 0;//一定要先清空!!!!!!!!!
                  for (var i = 0; i < x1.length; i++) {
                      var tempArray = x1[i];
                      downlist.options[i] = new Option(tempArray);
              }
           }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值