I have 3 textboxes placed in a datalist control. User shall enter numeric values in first two text box and their sum should be calculated using javascript and displayed in third textbox. There will be almost 100 or more rows in datalist.
Example of the datalist :
I have written the following code but doesn't work :
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim dt As New Data.DataTable
Dim dr As Data.DataRow
For i = 0 To 100
dr = dt.NewRow
dt.Rows.Add(dr)
Next
Calculator.DataSource = dt
Calculator.DataBind()
End If
End Sub
When I run the page , the javascript doesn't calculate the value. It will be highly appriciated if anyone could help :-)
shabeerna200...
0 Points
7 Posts
calculating the values of textbox in a datalist using javascript
Apr 14, 2012 11:34 AM|LINK
Hi,
I have 3 textboxes placed in a datalist control. User shall enter numeric values in first two text box and their sum should be calculated using javascript and displayed in third textbox. There will be almost 100 or more rows in datalist.
Example of the datalist :
I have written the following code but doesn't work :
HTML Page :
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> <script language="javascript"> function addNumbers() { var val1 = parseInt(document.getElementById("Calculator_ctl00_value1").value); var val2 = parseInt(document.getElementById("Calculator_ctl00_value2").value); var ansD = document.getElementById("Calculator_ctl00_answer"); ansD.value = val1 + val2; } </script> </head> <body> <form id="form1" runat="server"> <asp:DataList ID="Calculator" runat="server" RepeatLayout="Flow"> <ItemTemplate> <asp:TextBox ID="value1" runat="server" name="value1" onkeyup="javascript:addNumbers()" value="1"></asp:TextBox> <asp:TextBox ID="value2" runat="server" name="value2" onkeyup="javascript:addNumbers()" value="2"></asp:TextBox> <asp:TextBox ID="answer" runat="server" name="answer" ReadOnly="True"></asp:TextBox> </ItemTemplate> </asp:DataList> </form> </body> </html>Code behind the page (VB.NET)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then Dim dt As New Data.DataTable Dim dr As Data.DataRow For i = 0 To 100 dr = dt.NewRow dt.Rows.Add(dr) NextCalculator.DataSource = dt Calculator.DataBind() End If End SubWhen I run the page , the javascript doesn't calculate the value. It will be highly appriciated if anyone could help :-)