Changing textbox value should update hidden field
I have a hidden field which i want to update everytime when my textbox value is changed. It doesnt matter whether i am changing value at server side or client side when the textbox value changes hidden field should be updated. I have used onchange event of textbox but it does not work as i am 开发者_JAVA技巧changing values of textbox programatically. How can i do this?
You need to do it separately.
In the code behind you need to add in the TextBox_TextChanged
event
HiddenField.Value = TextBox.Text
And in the client side you need to add the onchange event like onchange="javascript:updateHiddenField();"
and then do something like:
function updateHiddenField() {
document.getElementById('HiddenFieldClientID').value = document.getElementById('TextBoxClientID').value
}
Beware that if you use MasterPages or databinding controls like repeaters/gridviews etc then the ClientID won't be the same as the ID.
A hidden field defined thusly:
<input type="hidden" id="BillMeFormReferred" name="referred" />
needs to pull the value of a textbox that is contained by a different form - that is, the hidden field is on a form being submitted - the text box sits in a different form _not_being_submitted.
jQuery is able to accomplish this via a button coded thusly:
<input type="button" value="Bill Me" id="BillMe"
onclick="$('#BillMeFormReferred').val( // the setter form of .val
$('#tbReferred').val() // references the remote field ID
); $('#BillMeForm').submit();" />
精彩评论