Like many others, I'm trying to invoke a .NET control's server-side event from JavaScript.
Specifically, I want to fire the TextChanged event on a TextBox named txtSearch. Therefore, I'm looking to reach the following event from client-side:
protected void txtSearch_TextChanged(object sender, EventArgs e)
Having read many answers on SO (for example here and here) I have the following JavaScript:
__doPostBack('ctl00$ctl00$Container$Main$txtSearch', 'TextChanged');
But the server-side event never fires.
I've tried numerous permutations: with the AutoPostBack true and false, with and without the event declared in the server-side instructions on the ASPX (i.e. OnTextChanged=""), with the EventValidation turned off in the page declaration, using the ClientID rather than the UniqueID in the EVENTTARGET parameter... but the event is still never fired.
A couple of other points
- the
txtSearchbutton control is also the trigger for an UpdatePanel, in case that matters. - I'm converting existing code, of which there's quite a lot, and am looking for something I can drop onto each page rather than converting the code-behind events to PageMethods.
Can anyone tell me what more I need to do?
I've tried this, and it works for me:
The server side
asp:TextBox, and the client sideinputwhich fires__doPostBackon click. The__doPostBackscript is generated throughPostBackOptions:The
txtSearch_TextChangedevent handler fires when the value of text box is changed, and thesubmitbutton is clicked.But note, for controls like
TextBoxthe text is stored in the viewstate; the new text entered by the user stores in the form data. When theTextBoxload the viewstate data, it gets the old value. This is compared with the new value that comes in the form. If the values are different, theTextChangedevent is fired. If no, the event handler won't be fired. This is the reason for thePage_Loadevent get fired (postback occured), buttxtSearch_TextChangeddidn't fire due to the value of theTextBoxdidn't change.