I have this asp:hiddenField control:
<asp:HiddenField ID="AuxHiddenField" runat="server"/>
I would like to trigger its (server side) value changed event using Jquery:
Protected Sub AuxHiddenField_Changed(ByVal sender As Object, ByVal e As System.EventArgs) Handles AuxHiddenField.ValueChanged
'some logic here
End Sub
I tried:
$('#AuxHiddenField').val("Mohaha!!");
Jquery fails to find the element and nothing happens.
How do I solve this?
ASP.net uses an algorithm to generate
ClientID. By default it is based on the naming container’s the control is contained in successively.It’s likely the ID in generated HTML is not
AuxHiddenFieldbut something likectl00_AuxHiddenField.Either set the ClientIDMode:
Or in jquery, using attirute selectors:
I personnaly don’t like the
<%= Field.ClientID %>way of doing this because if your javascript is in a separate file, it won’t be processed by asp.net.Further reading:
Changing programmatically a value with javascript does not fire the
changeevent, you have to trigger it manually:In action here.
About the ValueChanged event
Unfortunately, the HiddenField does not have the
AutoPostBackproperty (makes sense afterall).I think you will have to trigger a postback programmatically after changing the value.
If you are working ajax, use
__doPostBack(), otherwise, submit your formdocument.forms[0].submit().Another solution would be to replace the HiddenField by an invisible TextBox:
Changing the value programmatically will raise the event server-side.
Please note that for hiding the textbox, you should not use
Visible="falsebecause then it is not rendered in final html, use theStyle="display:none;"property (css).