Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have 2 Dropdownlist and I want to call this Javascript function in ASP.Net using my dropdownlist ID.how can I get this value, The javascript function is disabling the selected item from the dropdownlist so it cannot be selected to other dropdownlist.....

..........this the script tag that I get from the internet Ive paste my dropdownlist ID in here it is working but' if I save the value to database it resets, I think I'm not calling the DropDownlist ID in the right way can someone help me. on how to call the DropDownList ID in the javascript, asp.net way, thank you.

ASP.NET
<asp:DropDownList ID = "ddlHidden1" runat="server" >
               <asp:ListItem value="0">Select
               <asp:ListItem value="1">1
               <asp:ListItem value="2">2
               <asp:ListItem value="3">3
               <asp:ListItem value="4">4
               <asp:ListItem value="5">5
              </asp:DropDownList>
                           
                            
                    <asp:DropDownList ID = "ddlHidden2" runat="server">
               <asp:ListItem value="0">Select
               <asp:ListItem value="1">1
               <asp:ListItem value="2">2
               <asp:ListItem value="3">3
               <asp:ListItem value="4">4
               <asp:ListItem value="5">5    
                  </asp:DropDownList>
                 



    <asp:Button ID=btnSave runat="server" Text="Save" OnClick="btnSave_Click" />

    <script type=text/javascript>
        var ids = ['ddlHidden1', 'ddlHidden2'];

        NodeList.prototype.forEach = HTMLCollection.prototype.forEach =    
        Array.prototype.forEach;
        var selected = [];
        ids = makeid(ids);
        var opts = find(ids);
        updateS(opts);
        massdisabler(opts);
        assignFunc(ids, disabler);

        function assignFunc(i, func) { //done
        var x;
        for (x = 0; x < i.length; ++x) {
        i[x].onchange = function () {
        func(this);
        };
        } 
       }

       function disabler(i) {
       console.log(selected);
       updateS(opts);
       massdisabler(opts);
       }

       function makeid(i) { //done
       var o = [];
       i.forEach(function (v) {
       o.push(document.getElementById(v))
       });
       return o;
       }
 
       function find(ids) { //done
        var o = {};
        for (var i = 0; i < ids.length; ++i) {
       o[i] = {};
        ids[i].children.forEach(function (v, n) {
        o[i][n] = v;
       });
      }

       return o;
      }

      function massdisabler(op) { //done
      var y = Object.keys(op).length;
      for (var x = 0; x < y; x++) {
       var t = 0,
        l = Object.keys(opts[x]).length;
        for (var v = 0; v < l; v++) {
        t = op[x][v];

        if (selected.some(function (k) {
            if (t.value == k) {
                return true;
        }
        })) {
            t.setAttribute("disabled", "disabled");
        } else {
            t.removeAttribute("disabled");
          }
        }
       }
     }

      function updateS(op) { //done
      var s = [],
        y = Object.keys(op).length;

       for (var x = 0; x < y; x++) {

        var t = 0,
            l = Object.keys(opts[x]).length;

        for (var v = 0; v < l; v++) {
            t = op[x][v];
            var yn = s.some(function (k) {
               if (t.value == k) {
                return true;
                }
            });
            if (t.selected && (!yn)) s.push(t.value);
          }
       }
       selected = s;
       }
      </script>>
Posted
Updated 17-Jul-16 1:10am
v6
Comments
ZurdoDev 15-Jul-16 8:11am    
I don't follow what you are asking.
Nehru Gunter 16-Jul-16 8:37am    
Hello sir Gud evening can I ask you something I just need help with this one..

Nehru Gunter 16-Jul-16 21:28pm    
Sir can you give me example
Nehru Gunter 16-Jul-16 21:35pm    
Actually Sir What I want is a Dropdownlist that if you select the value from the First DropDownList You Cannot Select it From The Second DropDownList the same with The Second Dropdownlist if you select a value, you cannot select it to the First DropDownList, so Whenever I save a value that I select from the Selected value from the Dropdownlist I can avoid duplicate from the Database. I'm doing these for 2 weeks....

The Javascript that I've posted Is a function to Disable the Selected value from the DropDownlist to avoid duplicates, But the problem is whenever I Press the button save a selected value from the Dropdownlist, Both of the dropdownlist is returning to default value 0 so whenever, I try to save the value it always insert 0 in the column of database, but when I remove this javascript function, I can select the value that I want, and save it correctly, but you can select duplicate numbers, so I want to fix this javascript, so I'm asking maybe I'm not calling the javascript in the right way I'm assuming that whenever press the save button it always Reload and resets the value so it always save the value both 0.
Nehru Gunter 16-Jul-16 8:47am    
Actually Sir What I want is a Dropdownlist that if you select the value from the First DropDownList You Cannot Select it From The Second DropDownList the same with The Second Dropdownlist if you select a value, you cannot select it to the First DropDownList, so Whenever I save a value that I select from the Selected value from the Dropdownlist I can avoid duplicate from the Database. I'm doing these for 2 weeks....

The Javascript that I've posted Is a function to Disable the Selected value from the DropDownlist to avoid duplicates, But the problem is whenever I Press the button save a selected value from the Dropdownlist, Both of the dropdownlist is returning to default value 0 so whenever, I try to save the value it always insert 0 in the column of database, but when I remove this javascript function, I can select the value that I want, and save it correctly, but you can select duplicate numbers, so I want to fix this javascript, so I'm asking maybe I'm not calling the javascript in the right way I'm assuming that whenever press the save button it always Reload and resets the value so it always save the value both 0.

1 solution

Introduction to ASP.NET inline expressions in the .NET Framework[^]
HTML
<script type="text/javascript">
  var ids = ['<%=ddlHidden1.ClientID %>', '<%=ddlHidden2.ClientID %>'];
</script>
 
Share this answer
 
Comments
Nehru Gunter 17-Jul-16 9:52am    
You're a great man hope it works

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900