I am hoping there is an answer for this...
Setup: Pretty complex ASP.NET Application doing a bunch of user interaction, some of which requires that Javascript disables some fields when others are clicked (had to do it in Javascript, b/c the postback issues in ASP.NET made it impossible, so had to it client side).
That code is all well where Javascript does the classic document.getElementById(currObj).disabled = true;
The problem: When I am ready to submit the page, how can I make ASP.NET (C#) notice if a field (Textbox or Radio box) has been disabled client side, so it does NOT submit that field in the form? Is this even possible?
Much thanks in advance!
Any disabled fields don't submit but normal and even hidden ones do. This is how disabled fields work.
<input type="text" name="Test_1" value="off" disabled="disabled" />
<input type="text" name="Test_2" value="on" />
<input type="hidden" name="Hidden_1" value="hide_off" disabled="disabled" />
<input type="hidden" name="Hidden_2" value="hide" />
In the example only Test_2
and Hidden_2
will submit because the others are disabled.
You can see it at work in this jsfiddle: http://jsfiddle.net/bJ7Rq
The only way ASP.NET knows that a control has been disabled is if it was disabled from .NET and the Enabled = false
property has been written to ViewState. When you disable a field on the client-side, this isn't the case, so ASP.NET will retain the default Enabled
value.
As previous commenters have mentioned, the value of a disabled field will be completely excluded from the POST body sent to the server on postback. In the case of a basic TextBox
on a page, that would mean the Text
property would revert to String.Empty
. To differentiate between an empty string in an enabled field and an actual disabled field, I think you'd need to look directly at the Request.Form
object to see if the key exists. For example, if I had a TextBox
named myTextBox
on a blank page, Request.Form["myTextBox"]
would be null
for a disabled field. For an enabled field, it'd be whatever string value the field contained, including String.Empty
if it were blank. Of course, if the control is in a naming container (eg, a ContentPlaceholder
or something) that messes with the name
property on the actual field in HTML, you'd need to use the full HTML name
.
So for example:
bool fieldIsEnabled = Request.Form["myTextBox"] == null;
It's not elegant, but it should get the job done (excluding checkboxes, which would also be null
if the box wasn't checked).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.