Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I write a JavaScript function for sum calculation. it works but when I click add button to insert in to database it clear label value and i can't get label value to code page my JavaScript code is
HTML
<script type="text/javascript">
function sum()
{
    var sales_cost = document.getElementById("<%=txtsales_cost.ClientID%>").value;
    var vat = document.getElementById("<%=ddvat.ClientID%>").value;
    document.getElementById("<%=txtnetamt.ClientID%>").innerHTML=parseInt(sales_cost)+parseInt(sales_cost)*vat/100; 
}
</script>

I call it at button click
XML
<asp:Button ID="Button1" runat="server" CssClass="button" Text="Add" Width="79px" onclick="Button1_Click1" />
Posted
v4
Comments
Sampath Lokuge 28-Jan-14 2:13am    
At what time you call the 'Sum' function ? Please put that code snippet also ?
Is "txtnetamt" a TextBox or Label?
dhiraj mane 28-Jan-14 3:23am    
<asp:Button ID="Button1" runat="server" CssClass="button" Text="Add"
Width="79px" onclick="Button1_Click1" onclientclick="return Sum()" />
dhiraj mane 28-Jan-14 3:24am    
when i click button1 it clear txtnetamt label value

1 solution

Hello,
Question is not very clear but understand you can try these to solve your issue

Option1:
C#
<asp:button id="Button1" runat="server" cssclass="button" text="Add" xmlns:asp="#unknown">
                Width="79px" onclick="Sum()" /></asp:button>


option2:

C#
<asp:button id="Button1" runat="server" cssclass="button" text="Add" xmlns:asp="#unknown">
                Width="79px" onclick="javascript:Sum()" />
</asp:button>


option3:If you want server side and client side call
C#
<asp:button id="Button1" runat="server" cssclass="button" text="Add" xmlns:asp="#unknown">
                Width="79px" onclick="Button1_Click1" onclientclick="return Sum()" />
</asp:button>


Change your Script:

C#
function sum()
{
    var sales_cost=document.getElementById("<%=txtsales_cost.ClientID%>").value;
    var vat=document.getElementById("<%=ddvat.ClientID%>").value;
                    document.getElementById("<%=txtnetamt.ClientID%>").innerHTML=parseInt(sales_cost)+parseInt(sales_cost)*vat/100;
return false;
}
 
Share this answer
 
Comments
dhiraj mane 28-Jan-14 3:27am    
it's not working....
herei put my full code

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Product_Master.aspx.cs" Inherits="Master_Company_Master" Title="Product Master" %>

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<link href="../StyleSheet.css" rel="stylesheet" type="text/css" />

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script type="text/javascript">
function sum()
{
var sales_cost=document.getElementById("<%=txtsales_cost.ClientID%>").value;
var vat=document.getElementById("<%=ddvat.ClientID%>").value;
document.getElementById("<%=txtnetamt.ClientID%>").value=parseInt(sales_cost)+parseInt(sales_cost)*vat/100;
return false;
}
</script>
<table class="DataEntry_Table">
<tr>
<td class="TableHeader" colspan="2" style="text-align: center">
Product  Master Add</td>
</tr>
<tr>
<td class="style18">
Product Name</td>
<td class="style17">
<asp:TextBox ID="txtproduct" runat="server" Width="290px" CssClass="Textbox">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtproduct" ErrorMessage="*" SetFocusOnError="True">*
</td>
</tr>
<tr>
<td class="style18">
Description</td>
<td class="style17">
<asp:TextBox ID="txtdis" runat="server" Width="290px" CssClass="Textbox"
TextMode="MultiLine">
</td>
</tr>
<tr>
<td class="style18">
Sales Rate</td>
<td class="style17">
<asp:TextBox ID="txtsales_cost" runat="server" Width="110px" onchange="sum();" CssClass="Textbox">
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtsales_cost" ErrorMessage="*" SetFocusOnError="True">*
</td>
</tr>
<tr>
<td class="style18">
Category</td>
<td class="style17">
<asp:DropDownList ID="ddcategory" runat="server" CssClass="Textbox"
Height="25px" Width="108px">
<asp:ListItem>Tyre
<asp:ListItem>Tube
<asp:ListItem>Flap

<cc1:ListSearchExtender ID="ddcategory_ListSearchExtender" runat="server"
Enabled="True" TargetControlID="ddcategory">

</td>
</tr>
<tr>
<td class="style18">
Vat</td>
<td class="style17">
<asp:DropDownList ID="ddvat" runat="server" CssClass="Textbox" onchange="sum();" Height="25px"
Width="108px">
<asp:ListItem>0
<asp:ListItem>5
<asp:ListItem>12.5

</td>
</tr>
<tr>
<td class="style18">
Company </td>
<td class="style17">
<asp:DropDownList ID="ddcompany" runat="server" CssClass="Textbox"
Height="25px" Width="292px">

<cc1:ListSearchExtender ID="ddcompany_ListSearchExtender" runat="server"
Enabled="True" TargetControlID="ddcompany">

<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ControlToVali
dhiraj mane 28-Jan-14 3:29am    
i make small change on my page i take TextBox for txtnetamt ... it works when my txtnetamt is readonly false... i want to make it when readonly is true..pls help
karthik Udhayakumar 28-Jan-14 5:18am    
Not clear ..can you pls explain!
dhiraj mane 30-Jan-14 0:16am    
This is my javascript code
<script type="text/javascript"> function sum() { var sales_cost=document.getElementById("<%=txtsales_cost.ClientID%>").value; var vat=document.getElementById("<%=ddvat.ClientID%>").value; document.getElementById("<%=txtnetamt.ClientID%>").value=parseInt(sales_cost)+parseInt(sales_cost)*vat/100; return false; } </script>

I call It at Add Buttom Click that time calculation of parseInt(sales_cost)+parseInt(sales_cost)*vat/100 is show into textbox txtnetamt it work successful when txtnetamt is enable true when i make it enable false then at button click it clear value.....and i cna't get value at add button_click

<asp:Button ID="Button1" runat="server" CssClass="button" Text="Add" xmlns:asp="#unknown"
Width="79px" onclick="Button1_Click1" ValidationGroup="a" />

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