Click here to Skip to main content
15,945,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to get id of server control using javascript..
I use following code..
var rcno = document.getElementById('<%=recno.ClientID %>');
But it return null value.. I think before button click, server control visibility is false so it return null value.. both server control i.e. textbox and button are on same page..
I want to access server control's value on button click and also textbox's visibility false..
plz suggest me how I get value..

Thanks in advance..

One way is instead of setting TextBox Control

So that it makes the Control to Render on the Page and can be accessed as usual with document.getElementById.

Other way is to use Hidden Fields to store the value of TextBox in case you are using Visible false option. Then you should fetch the Hidden Field value through Javacript.
Share this answer
heta.dave 29-Jan-14 1:48am    
Many many thanks for rply..
I have to use visibility false option.
but when I use hidden field It also return null value..
Plz suggest me how to fetch hidden field value, may be I use wrong code ..

Thanks in advance..
JoCodes 29-Jan-14 3:50am    
Can you share the code you tried?. I ll have a look
heta.dave 29-Jan-14 7:39am    
code foe hidden field: <input type="hidden" runat="server" id="inphide" value="0" />

on button click I access value of hidden field through javascript function as
var rcno = document.getElementById('<%=inphide.ClientID %>').value;

and in .cs page I access hidden field value as
inphide.Value = recno.Value;
where recno is textbox control whose id I want to fetch in javascript function

Hi Heta

if the Server control's Visibilty is set as false
recno.Visible = false;

The control wont be rendered in the screen(page). so the control will not be present on the page DOM[^]
So the Control will be null on the Javascript..

You can try like this..
make the CSS Visibility[^] as Hidden in it..
recno.Style.Add("visibility", "hidden");

<asp:textbox id="recno" style="visibility: hidden" runat="server" xmlns:asp="#unknown"></asp:textbox>
Share this answer
Access like this:

var valueoftextbox = document.getElementById('<%= txtBox1.ClientID %>').value

document.getElementById('<%= txtBox1.ClientID %>').style.display = "block" --> Display

document.getElementById('<%= txtBox1.ClientID %>').style.display = "none" --> Not to Display
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900