Hi there
Here is the situation - a table is built dynamically with values in the cells I need to retrieve and enter into a database. I have found a way of getting values using JavaScript, but of course with the table being dynamic I have no idea how many rows it will contain. One way is using the TableID.rows.co unt and then building if statements based on the value - but that is going to be an awfull lot of coding...... So the question is - is there an easier way to retrieve the values? I know that building an array is the answer, but actually getting from A to B is giving me the headache. Any pointers would be appreciated!
Here is the JavaScript that works - but has the limits above. Any ideas would be appreciated! -
-
Dim GetValues As String = "<script language='JavaScript'>" & _
-
"var table= document.getElementById('Table1'); " & _
-
"var nRows = new Array(); " & _
-
"nRows[0] = table.rows[2]; " & _
-
"nRows[1] = table.rows[3]; " & _
-
"nRows[2] = table.rows[4]; " & _
-
"nRows[3] = table.rows[5]; " & _
-
"nRows[4] = table.rows[6]; " & _
-
"nRows[5] = table.rows[7]; " & _
-
"nRows[6] = table.rows[8]; " & _
-
"nRows[7] = table.rows[9]; " & _
-
"nRows[8] = table.rows[10]; " & _
-
"nRows[9] = table.rows[11]; " & _
-
"var ID1 = nRows[0].cells[0]; " & _
-
"var ID2 = nRows[1].cells[0]; " & _
-
"var ID3 = nRows[2].cells[0]; " & _
-
"var vID1 = ID1.innerText; " & _
-
"var vID2 = ID2.innerText;" & _
-
"var vID3 = ID3.innerText; " & _
-
"var vQty1 = document.getElementById('ctl00_txt0').value ; " & _
-
"document.getElementById('Label1').innerText = vID1 ; " & _
-
"document.getElementById('Label2').innerText = vID2 ; " & _
-
"document.getElementById('Label3').innerText = vID3 ; " & _
-
"document.getElementById('Label4').innerText = vQty1 ; " & _
-
"</script>"
-
-
GetValues = GetValues.Replace("Table1", Table1.ClientID).Replace("Label1", Label1.ClientID).Replace("Label2", Label2.ClientID).Replace("Label3", Label3.ClientID).Replace("Label4", Label4.ClientID)
-
Page.ClientScript.RegisterStartupScript(Me.GetType(), "GetValues", GetValues)
-
-
7 21480 acoder 16,027
Recognized Expert Moderator MVP
Define a tbody under your table that encapsulates your TRs and TDs.
Then, assuming an id of "tbody", use the following code to retrieve the current row count: - var currRow = document.getElementById("tbody").rows.length;
acoder 16,027
Recognized Expert Moderator MVP
Actually, I'm not sure if defining tbody is necessary, but it works for me. Give it a shot.
I can get the rows simply by :- - var totalrows = table.rows.length;
The problem I've got is how to define the array (once the total number of rows are known) and then get the variables into a database.
BTW, the first two rows are only headers and don't contain data, and the last row contains a total in cell 3.
acoder 16,027
Recognized Expert Moderator MVP
Why don't you just define the array when constructing the table in your ASP (I presume you're using ASP?) code. You can ignore the first two rows when populating the array with values. Then you can also have a variable which stores the total value that you mention. The database problem is totally ASP-related, nothing to do with javascript.
Having re-read my initial posting, it does seem rather ambiguous. I apologise for that.
Here is a better description of the dynamic table:-
Row 0 is the header
Row 1 contains description in 6 columns
Row 2 onwards contain the dynamic rows. Cell 2 contains an <input> textbox (not a TextBox Control).
The final row contains a total in cell 3
Under the me.load event I then need to run a script to first find how many rows, then get those rows into an Array, then manipulate the data from that array (update a database, send to another page..)
I do know that by using:- var table= document.getEle mentById('Table 1');
I can get the table rows and cells, and by using var totalrows = table.rows.leng th; (Thanks acoder :-) )
I can get the total number of rows
I also know that var nRows = new Array();
nRows[0] = table.rows[2]
defines the first set of values in the first column (I can retrieve the other values back from the database once I have the ID number from this column)
and that I can get the value of the first TextBox (that gives the quantity) with vQty1 = document.getEle mentById('txt0' ).value ;
each textbox in the row has an incremented ID value - txt0, txt1, txt2 and so on
Where I hit the buffers is formatting an array to hold the data, then finding a way of retrieving it again!
I have come across the following snippet :- for(var i = 0; i < x.x.length; i++)
Whilst I know there is a key there somewhere,,,, sigh
Why don't you just define the array when constructing the table in your ASP
The main problem is the table is fomed in a .NET MasterPage - the processing page is the content. .NET loads the processing page first, then the masterpage, so it's impossible to capture the variables in VB - since JavaScript runs AFTER the full page load (and I can do that in VB code behind) I will get the variables that have loaded AFTER the processing page. (Otherwise VB will always return a value of 3 for the rows (the two headers and the footer).
This seems rather messy, but it gets values up to a max. pre-defined amount of rows into a container to process!
If anyone knows an easier way (and I'm sure there is) I'd love to know about it! -
-
-
Dim vTotalRows As Integer = Request.QueryString("vID")
-
HiddenField1.Value = CStr(Request.QueryString("total"))
-
Dim Table1 As HtmlTable = Master.FindControl("Table1")
-
Dim vValue1 As String = CType(Me.Page.Master.FindControl("TextBox3"), TextBox).Text
-
TextBox3.Text = vValue1
-
-
If vTotalRows > 3 Then
-
'start JavaScript
-
Dim GetValues As String = "<script language='JavaScript'>" & _
-
"var table= document.getElementById('Table1'); " & _
-
"var totalrows = table.rows.length; " & _
-
"var i = (totalrows - 3) ; " & _
-
"var nID = new Array(i); " & _
-
"nID[0] = table.rows[2].cells[0].innerText; " & _
-
"var vQty1 = document.getElementById('ctl00_txt0').value ; " & _
-
"if (i > 1) { " & _
-
"nID[1] = table.rows[3].cells[0].innerText; " & _
-
"var vQty2 = document.getElementById('ctl00_txt1').value ; " & _
-
"}" & _
-
"else { " & _
-
"nID[1] = ''; " & _
-
"var vQty2 = '' ;" & _
-
"}" & _
-
"if (i > 2) { " & _
-
"nID[2] = table.rows[4].cells[0].innerText; " & _
-
"var vQty3 = document.getElementById('ctl00_txt2').value ; " & _
-
"}" & _
-
"else { " & _
-
"nID[2] = '' ;" & _
-
"var vQty3 = '' ;" & _
-
"} " & _
-
"if (i > 3) { " & _
-
"nID[3] = table.rows[5].cells[0]; " & _
-
"var vQty4 = document.getElementById('ctl00_txt3').value ; " & _
-
"} " & _
-
"else { " & _
-
"nID[3] = '' ; " & _
-
"var vQty4 = '' ; " & _
-
"}" & _
-
"if (i > 4) { " & _
-
"nID[4] = table.rows[6].cells[0]; " & _
-
"var vQty5 = document.getElementById('ctl00_txt4').value ; " & _
-
"} " & _
-
"else { " & _
-
"nID[4] = ''; " & _
-
"var vQty5 = '' ; " & _
-
"} " & _
-
"if (i > 5) { " & _
-
"nID[5] = table.rows[7].cells[0]; " & _
-
"var vQty6 = document.getElementById('ctl00_txt5').value ; " & _
-
"} " & _
-
"else { " & _
-
"nID[5] = '' ; " & _
-
"var vQty6 = '' ; " & _
-
"}" & _
-
"if (i > 6 ) { " & _
-
"nID[6] = table.rows[8].cells[0]; " & _
-
"var vQty7 = document.getElementById('ctl00_txt6').value ; " & _
-
"} " & _
-
"else { " & _
-
"nID[6] = '' ; " & _
-
"var vQty7 = '' ; " & _
-
"}" & _
-
"document.getElementById('Label1').innerText = nID[0] ; " & _
-
"document.getElementById('Label2').innerText = nID[1] ; " & _
-
"document.getElementById('Label3').innerText = nID[2] ; " & _
-
"document.getElementById('Label4').innerText = vQty1 ;" & _
-
"document.getElementById('Label5').innerText = vQty2 ; " & _
-
"document.getElementById('Label6').innerText = vQty3 ; " & _
-
"</script>"
-
'end JavaScript
-
GetValues = GetValues.Replace("Table1", Table1.ClientID).Replace("Label1", Label1.ClientID)
-
GetValues = GetValues.Replace("Label2", Label2.ClientID).Replace("Label3", Label3.ClientID).Replace("Label4", Label4.ClientID)
-
GetValues = GetValues.Replace("Label5", Label5.ClientID).Replace("Label6", Label6.ClientID)
-
Page.ClientScript.RegisterStartupScript(Me.GetType(), "GetValues", GetValues)
-
Else
-
Label1.Text = "Table is empty!"
-
End If
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: js |
last post by:
I have a table rendered with XSLT. The first column has a radio
button controls for user to make a selection for a particular row.
All the values in the remaining columns are all concated with a  
(blank). I need to retieve the text in each cell of that row if the
radio button on that row is clicked. Each <TD> has a numeric ID so
that I can use it with document.getElementById().innerText method. I
use Javascript to assign some...
|
by: Eugfene |
last post by:
I have the following function in a html file:
function selectEdit(fileID, processCode, processID, fileName,fileDesc)
{
document.forms.recordID.value = fileID;
document.forms.processor.value = processCode; `
document.forms.processorID.value = processID;
document.forms.interfaceFileName.value = fileName;
document.forms.fileNameDesc.value=fileDesc;
document.forms.submit();
|
by: ggk517 |
last post by:
We are trying to develop an Engineering application using PHP,
Javascript with Informix as the back-end.
Is it possible to retrieve data using Javascript but by accessing the
Database. Say somebody enters part_no, than using Javascript is it
possible to connect to the part master and retrieve the division and
desc information?
I am not allowed to use the PHP because this will require the user to
insert the part number on the first...
|
by: no.mail.pls |
last post by:
Hiya,
How do i retreive fields with similar values from 2 tables?
I tried to use
(1) "SELECT * FROM $table1 as o , $table2 as p WHERE o.name like
'%p.name%'";
but it retrieves nothing at all.
(2) "SELECT * FROM $table1 as o , $table2 as p WHERE o.name like p.name";
|
by: Patricio |
last post by:
Hello all
I open a PopUp that receives two variables (values). This PopUp must return
a value as well.
How can I retrieve this value?
Thanks a lot.
| |
by: bazubwabo |
last post by:
hi everybody ,
could u please help me to find out the error on my asp codes,i can't retrieve the inserted values.
Here is below the code:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
connect_string = "Driver={SQL Server};Server=NLROSDBCL9-10;Database=Scanners;Uid=scanners;Pwd=bertrand;"
|
by: phpachu |
last post by:
Hi,
I hav created a group of textboxes using a loop and its names are unique and names are assigned using variable ( like <input type=text name=$name1>). Then How can i retrieve the values in that textboxes after a button click with in a loop. Really I cant move from here. Can anyone plz help me..
Part of the my code is given below
<html>
<table>
<?php
// i hav an array "assocArray" and its count is "countarray"
for($i = 1; $i <...
|
by: Evanescent |
last post by:
Hi Guys, I am trying to create a form which allows the users to retrieve records based on the values entered or chosen in the various combo boxes and textboxes, such as the customer's name, invoice number, service number and installation site. The users do not have to enter all the information for the search.
Currently, for the Customer's Name Combo Box, the values are retrieved from the Information Table and when a user selects a name, the...
|
by: Archanak |
last post by:
Hi,
I have table like this:
select * from sampletest;
| title | description ...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |