Access textbox in client side javascript

kaisersoze

Centurion
Joined
Aug 27, 2003
Messages
152
Hello
I need to access (on client side using javascript) the value property of a asp:textbox, whose visible=false.
I was able to access the textbox if visible=true, but when i change it to false then i get undefined. When i right click and see the code also i dont see the textbox.
can some one help. even alternate approach of achieving this is appreciated.
 
drop an HTMLInputHidden (is that the name???) set runat=server
set its value to the value of the text box in your code behind.
refer to that in your client side script.
add, if need be, register clientside script in the textbox prerender that sets the hidden value during the TextBox onchange event.
 
Another option

I use a lot of hidden inputs (HtmlInputHidden) to do some client-side trickery but there is another solution.

Another option is to "hide" your ASP:Textbox with styles instead of setting visibility. When you set visible to false on an ASP:Control it doesn't get rendered (I think its value is kept in ViewState - which does you no good for client-side scripting). So...

Instead of setting visibility to "hide" your textbox give it a display style of none.

Code:
//  C# example - it's the same in VB.Net without the semi-colon.
txtWhatever.Style.Add("display", "none");  //  Hides it, but it's still scriptable.

Note that if your control already has a display style (for ex, if you set that particular style in your .aspx) that you can get at it with collection indexing instead:

Code:
txtWhatever.Style["display"] = "none";  //  hides it
txtWhatever.Style["display"] = "inline";  //  makes it visible

Sometimes I might make a control hidden with a display style of none in my aspx but client or server side events could cause it to become visible, which I do by resetting the display style to inline.

Code:
//  Javascript to make the textbox appear:
document.all.txtWhatever.style.display = 'inline';

Anyways - elements that have a display style of "none" can be manipulated via client-side javascript but they aren't visible.

Paul
 
PWNettle said:
I use a lot of hidden inputs (HtmlInputHidden) to do some client-side trickery but there is another solution.

Another option is to "hide" your ASP:Textbox with styles instead of setting visibility. When you set visible to false on an ASP:Control it doesn't get rendered (I think its value is kept in ViewState - which does you no good for client-side scripting). So...


Paul
Damn! never thought of that. . . Learn something new everyday.
 
Back
Top