views:

150

answers:

2

I have a text box control within a Grid Control. I would like to get the clientID of the textBox using javascript with something like "<%= txtBox.ClientID %>". But I get an error message saying that txtBox does not exist in the current context. The textbox is programmatically created.

Could you let me know how to get the clientID of the textBox.

Thanks

+1  A: 

UPDATE: this should work better: <%= myContainer.FindControl("txtBox").ClientID %>


One way would be to put it in a hidden field when you generate the text box:

  TextBox txtBox = new TextBox();
  txtBox.ID = "txtBox";
  Page.ClientScript.RegisterHiddenField("txtBoxClientID", txtBox.ClientID);

and then you can get it on the client-side using

document.getElementById('txtBoxClientID').value
Veli
A: 

instead of this <%= txtBox.ClientID %>

jst try out

document.getElementById('ID_OF_THE_CONTROL*').value;

*..the id of the textbox..

Neerav