views:

77

answers:

2

If you create a table and add it, the ClientID property in the code behind doesn't reflect the ID in the actual file, so document.getElementById doesn't work.

How can I add a control, and be able to access it using Javascript, with only its ID (such as 'table1')?

+2  A: 

You will have to render the "ClientId" out to the browser and then grab that value to be able to use it via Javascript.

Starting with .NET 4.0, this will no longer be an issue as you can change the behavior of the iNamingContainer and the id structures.

Mitchel Sellers
The problem is, the ClientID and the ID are the same when I create the table and add it to the html file, it's only later on in another method that the ClientID is correct...
SLC
Can you then show us how you are adding this? You need to get the client ID AFTER you add it to the respective container, so that the client ID is valid.
Mitchel Sellers
I see, I think I have it now, thanks ;)
SLC
A: 

You could set a javascript variable on the fly to your new table's ClientID and then use its value in getElementById.

LymanZerga