user1369925
user1369925

Reputation: 55

How to find a asp control id using java script?

I am using following code to find control id.

alert(document.getElementById("<%# TextBox1.ClientId %>").value )

but this code is giving error "object required". Please help me.

Upvotes: 1

Views: 16242

Answers (5)

Habib
Habib

Reputation: 223257

Either use

alert(document.getElementById("<%= TextBox1.ClientId %>").value )

or set ClientIDMode="Static" for textbox and then

alert(document.getElementById("<%= TextBox1 %>").value )

Also check How to: Access Controls from JavaScript by ID

Upvotes: 0

Josh Darnell
Josh Darnell

Reputation: 11433

You need to use '=', not '#'

alert(document.getElementById("<%= TextBox1.ClientId %>").value );

The "<%#" symbol is an inline expression used for databinding.

The "<%=" symbol there is used for display / translation purposes. It basically does a Response.Write of just the value of the .ClientID property of your server control.

See this article on inline expressions in asp.net for more info.

Upvotes: 3

Andrei
Andrei

Reputation: 287

In the context of JavaScript, which is on the client side "<%# TextBox1.ClientId %>" has no meaning because this will be translated by ASP into to a different ID based on how you configure TextBox1.ClientIDMode for which you have 4 modes described here Control.ClientIDMode

Upvotes: 0

Imran Rizvi
Imran Rizvi

Reputation: 7438

replace # with = in the given statement updated statement is

  alert(document.getElementById("<%= TextBox1.ClientId %>").value);

Upvotes: 1

Saurabh
Saurabh

Reputation: 5727

alert(document.getElementById('Id of control').value )

You can get the exact id of control by view source.

Upvotes: 0

Related Questions