Click here to Skip to main content
15,905,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have two labels and a dropdownlist..dropdownlist contains many numerical values.when user selects any dropdownlist value,that value should be multiplied with a constant number(which i retrieved here from database) and the result should change immediately on each user click. i used label7(hidden label) for retrieving a constant numerical value from database,and label4(visible label) for displaying the result after multiplication..here im getting a NaN as result...after writing the following code....

XML
<asp:Label ID="Label4" runat="server" style="color:white;font-size:medium;font-weight:bold;"></asp:Label><asp:Label ID="Label7" runat="server" style="color:white;font-size:medium;font-weight:bold;visibility:hidden;"></asp:Label>

<asp:DropDownList ID="DropDownList1" runat="server" Width="50px" onchange="mul()">
        <asp:ListItem>1
        <asp:ListItem>2
        <asp:ListItem>3
        <asp:ListItem>4
        <asp:ListItem>5
        <asp:ListItem>10
        <asp:ListItem>20
 

<script type="text/javascript">
      function mul() {
          var lab = document.getElementById('<%= Label7.ClientID %>');
          var lab1 = document.getElementById('<%= DropDownList1.ClientID %>');
 document.getElementById('<%=Label4.ClientID%>').innerHTML = parseInt(lab) * parseInt(lab1);
      }
      </script>
 finally my c# code
        Label4.Text = dt.Rows[0][3].ToString();
        Label7.Text =  dt.Rows[0][3].ToString();
Posted

Try This :

XML
<asp:DropDownList ID="DropDownList1" runat="server" Width="50px" onchange="mul()">
    <asp:ListItem Text="One" Value="1">One</asp:ListItem>
    <asp:ListItem Text="Two" Value="2">Two</asp:ListItem>
    <asp:ListItem Text="Three" Value="3">Three</asp:ListItem>
    <asp:ListItem Text="Four" Value="4">Four</asp:ListItem>
</asp:DropDownList>



Javascript Code :
-------------------
JavaScript
function mul() {
    var lab = document.getElementById('<% =Label7.ClientID %>');
    var lab1 = document.getElementById('<% =DropDownList1.ClientID %>');
    document.getElementById('<% =Label4.ClientID %>').innerHTML = parseInt(lab.innerHTML) * parseInt(lab1.value);
}
 
Share this answer
 
v3
1) You must give comprehensible names to your variables
2) You did not extract any value of your controls so far
3) You have to use display instead of visibility to retrieve the value of your label

C#
function mul() {
    var lblVisible = document.getElementById('<%= lblVisible.ClientID%>');
    var lblHiddenValue = document.getElementById('<%= lblHidden.ClientID%>').innerText;
    var selectedValueFromDdl = document.getElementById('<%= ddlSelection.ClientID %>').value;
    lblVisible.innerText = parseInt(lblHiddenValue) * parseInt(selectedValueFromDdl);
}


XML
<asp:Label ID="lblVisible" runat="server" style="color:white;font-size:medium;font-weight:bold;"></asp:Label>
<asp:Label ID="lblHidden" runat="server" style="color:white;font-size:medium;font-weight:bold;display:none;"></asp:Label>
<asp:DropDownList ID="ddlSelection" runat="server" Width="50px" onchange="mul()">
    <asp:ListItem>1</asp:ListItem>
    <asp:ListItem>2</asp:ListItem>
    <asp:ListItem>3</asp:ListItem>
    <asp:ListItem>4</asp:ListItem>
    <asp:ListItem>5</asp:ListItem>
    <asp:ListItem>10</asp:ListItem>
    <asp:ListItem>20</asp:ListItem>
</asp:DropDownList>
 
Share this answer
 
Try some changes:

<asp:ListItem>1</asp:ListItem>


document.getElementById('<%= Label4.ClientID %>').innerHTML = parseInt(lab.innerHTML) * parseInt(lab1.options[lab1.selectedIndex].value);
 
Share this answer
 
v2

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