I have an <asp:textbox on a form that is populated by users selecting from a clientside dropdown control listing out possible selections. If a certain selection meets a particular criteria, I lock the textbox using javascript so that the user can't change
it.
This all works fine ...
if(registered=="yes")
{
namecontrol.disabled = true;
}
The problem is when the form does postback, I can't read the value of the textbox because it's disabled. I've been reading that disabled controls do not retain viewstate and as a result their values are not sent to the server on postback. A possible solution
would be to have a second textbox on the form that is hidden (and not disabled) so that when the form posts back to the server I can read that value, but i would rather not do that. Seems a little hackish and I would like to find a way to use just one textbox
control.
Furthermore, it seems that if i was to set the enabled property of the textbox in serverside vb.net, the value would be retained on postback, but I cannot do that because the textbox becomes disabled clientside when a selection is made.
You cannot get the value of a field on server-side if it was disabled using javascript becuase it wont get sent with the postback (naturally). Instead of disabling, have you considered making the textbox invisible?
namecontrol.style.display = 'none';
Hope this helps! Don't forget to mark the most helpful post(s) as Answer for the sake of future readers. Thanks!
Well, i need to allow the user to manually enter/type a value (a company name) too. If they choose from the dropdown though, i check if the selected company is "registered". if so, i need to lock the textbox to prevent them from changing the value selected
from the dropdown.
The dropdown is just a panel that shows/hides on the form using ajax.
Well, this is what I had in mind. You have a textbox AND a label. By default, your label has display:none. When you want to make it read-only, set the textbox display to none, and set the label display to block. Then put your value in the label. You
can then switch back and forth when necessary based on the drop down selection.
Hope this makes sense to you, let me know if you need help.
ok, thanks for the info. i'll give it a shot and see how it works.
do you by any chance know how i could add an attribute to a control using javascript.
if i had the textbox control on the client like ...
<input type="text" id="txtTest" runat="server" />
how can i add the attribute readonly="readonly"? i'm just wondering if this would prevent the user from making changes to the textbox, but still post back the value to the server.
do i just use document.getelementbyid('txtTest').setAttribute("readonly", "readonly")
if so i don't think it works. oh wel, thought i would give it a shot ....
setAttribute is the correct method to call, but I still dont think your data will postback (im pretty sure the readonly behaves identically with disabled). Javascript is case-sensitive so make sure its getElementById.
Also, you dont need to call setAttribute for readonly, becuase there is already a property setup for that (just like disabled):
bschumacher@...
Member
3 Points
28 Posts
disabling asp:textbox using javascript
May 04, 2007 02:12 PM|LINK
Hello All,
I have an <asp:textbox on a form that is populated by users selecting from a clientside dropdown control listing out possible selections. If a certain selection meets a particular criteria, I lock the textbox using javascript so that the user can't change it.
This all works fine ...
if(registered=="yes")
{
namecontrol.disabled = true;
}
The problem is when the form does postback, I can't read the value of the textbox because it's disabled. I've been reading that disabled controls do not retain viewstate and as a result their values are not sent to the server on postback. A possible solution would be to have a second textbox on the form that is hidden (and not disabled) so that when the form posts back to the server I can read that value, but i would rather not do that. Seems a little hackish and I would like to find a way to use just one textbox control.
Furthermore, it seems that if i was to set the enabled property of the textbox in serverside vb.net, the value would be retained on postback, but I cannot do that because the textbox becomes disabled clientside when a selection is made.
Any help would be great!
Thanks,
Ben
JoshStodola
Star
13736 Points
3177 Posts
Re: disabling asp:textbox using javascript
May 04, 2007 03:26 PM|LINK
You cannot get the value of a field on server-side if it was disabled using javascript becuase it wont get sent with the postback (naturally). Instead of disabling, have you considered making the textbox invisible?
Hope this helps! Don't forget to mark the most helpful post(s) as Answer for the sake of future readers. Thanks!
bschumacher@...
Member
3 Points
28 Posts
Re: disabling asp:textbox using javascript
May 04, 2007 03:33 PM|LINK
JoshStodola
Star
13736 Points
3177 Posts
Re: disabling asp:textbox using javascript
May 04, 2007 03:35 PM|LINK
bschumacher@...
Member
3 Points
28 Posts
Re: disabling asp:textbox using javascript
May 04, 2007 03:40 PM|LINK
Well, i need to allow the user to manually enter/type a value (a company name) too. If they choose from the dropdown though, i check if the selected company is "registered". if so, i need to lock the textbox to prevent them from changing the value selected from the dropdown.
The dropdown is just a panel that shows/hides on the form using ajax.
JoshStodola
Star
13736 Points
3177 Posts
Re: disabling asp:textbox using javascript
May 04, 2007 03:45 PM|LINK
Well, this is what I had in mind. You have a textbox AND a label. By default, your label has display:none. When you want to make it read-only, set the textbox display to none, and set the label display to block. Then put your value in the label. You can then switch back and forth when necessary based on the drop down selection.
Hope this makes sense to you, let me know if you need help.
bschumacher@...
Member
3 Points
28 Posts
Re: disabling asp:textbox using javascript
May 04, 2007 03:56 PM|LINK
ok, thanks for the info. i'll give it a shot and see how it works.
do you by any chance know how i could add an attribute to a control using javascript.
if i had the textbox control on the client like ...
<input type="text" id="txtTest" runat="server" />
how can i add the attribute readonly="readonly"? i'm just wondering if this would prevent the user from making changes to the textbox, but still post back the value to the server.
do i just use document.getelementbyid('txtTest').setAttribute("readonly", "readonly")
if so i don't think it works. oh wel, thought i would give it a shot ....
JoshStodola
Star
13736 Points
3177 Posts
Re: disabling asp:textbox using javascript
May 04, 2007 04:08 PM|LINK
setAttribute is the correct method to call, but I still dont think your data will postback (im pretty sure the readonly behaves identically with disabled). Javascript is case-sensitive so make sure its getElementById.
Also, you dont need to call setAttribute for readonly, becuase there is already a property setup for that (just like disabled):
document.getElementById('txtTest').readOnly = true;I hope you can get your issue resolved.