I have an asp.net textbox which is disabled and I am setting the value for the te开发者_JAVA百科xtbox on client side using JQuery.
$('[id$=txtCity]').val('Naperville');
But the problem is when I am trying to save the data. If I say txtCity.Text it is not giving me the value that I set in the client side.
The textbox is disabled and I am seeing the value reflected in the textbox on the client side after the value is set using JQuery. But I am not sure why it is not reflected on the server side.
I found that TextBox with disabled will not be submitted with the form on postback. If that is true, how can I submit those values and get that on server side in the code behind.
Please help. Thanks and appreciate your feedback.
You can always "un-disable" the field before posting. E.g.,
$('form').submit(function() {
$(this).find(':input:disabled').removeAttr('disabled');
});
And of course using readonly
fields, as per Knix's answer, may be a viable solution as well.
Edit: just read your comment on Knix's answer, so it sounds like dynamically removing the disabled attribute before posting may be the way to go.
I don't think disabled fields will get posted.
Maybe you should try readonly
field: http://www.w3schools.com/tags/att_input_readonly.asp
<input id="firstName" name="firstName" value="hello world" READONLY>
You can always use CSS to make the readonly field appear disabled =)
EDIT:
Ok..if you cannot do the readonly, how about using AJAX to post and directly passing over the data since you say you can access it on the client side:
$.ajax({
url: "yourForm.php", // sorry I used PHP in this example
type: "POST",
data: { txtCity : $('#txtCity').val() },
success: function(msg){
alert(msg);
}
}
)
EDIT2:
How about populating a hidden field and using that value on the server-side since you cannot use readonly fields or use AJAX?
$('#hiddenField').val('Naperville');
Since this field is hidden, the user would not know the difference =)
EDIT3:
OP got this to work using correct server-side syntax for the readonly inputs:
TextBox1.Attributes.Add("readonly","readonly")
精彩评论