0

I create a table dynamically and add rows, labels etc to it. I want to be able to access those rows to make either visible or hidden AND access labels to change content on the fly. So far the table and all info is created with no problem. I spent days trying to access the data from JS, but I keep getting NULL etc on objects using ALERT to test it. Here's a snippet example of my code...

ASP.NET (C#) code

            mTable = new HtmlTable();
            mTable.ID = "mTable";

            aCell = new HtmlTableCell();

            aLabel = new Label();
            aLabel.ID = "aLabel";
            aLabel.Text = "TEST";

            aCell.Controls.Add(aLabel);

            aRow = new HtmlTableRow();
            aRow.ID = "r" + x;
            aRow.Cells.Add(aCell);
            mTable.Controls.Add(aRow);

Ive put the following code in a SCRIPT FILE etc and ive tried many styles.

           alert(document.getElementById('<%=aLabel.ClientID%>'));
F U
  • 71
  • 1
  • 8
  • This one thing that helped fix the problem. mTable.ClientIDMode = System.Web.UI.ClientIDMode.Static; – F U Sep 13 '15 at 05:31

1 Answers1

0

If you are using plain vanilla javascript, please look at the code sample here: How do I iterate through table rows and cells in javascript?

The code sample in the above link gets the table by id, which in your case is 'mTable' (from your c# code)

var table = document.getElementById('mTable'); 
// will return you a reference to the table object on the page

You also have to place the code to call your javascript function that accesses data on the 'mTable' on the document load event

Community
  • 1
  • 1
chrisl08
  • 1,572
  • 1
  • 11
  • 20
  • Thanks for the great answer. Where do you put this document load event anyways? I tried using that before but I could understand where to put it. I was trying to get this to work using a separate JS file and loaded it with Page.ClientScript.RegisterClientScriptInclude("script", "myScript.js"); One way I got something to work what by using mTable.ClientIDMode = System.Web.UI.ClientIDMode.Static – F U Sep 13 '15 at 05:34
  • I usually put the document load event inline at the bottom of my html page. No need for Page.ClientScript. Here's some more info on document.load: http://stackoverflow.com/questions/799981/document-ready-equivalent-without-jquery. Also, I would recommend using jquery to accomplish the task above, especially if it is a part of a real life project. Nobody does plain vanilla javascript these days, – chrisl08 Sep 13 '15 at 05:55