theSpyCry
theSpyCry

Reputation: 12293

ASP.NET jquery function call

Suppose an ASP.NET .NET 3.5 Site.

There is an asp:TextBox defined with this jquery javascript attached to it:

$("#myTextBox").change(function() {});

which means that if the user changes the text value, this function will be called. And of course it works.

The problem is that I've made a button in which's EventHandler it automatically fills this TextBox with the default value. A round trip / page reload occurs and a new value is correctly set. With the difference, that the jquery function change() is not called;

And i need to call that function because it sets a variable to decide whether the text has been changed.

EDITED:

The variable is set to check if the user provided some input. If that's the case, it offers him the possibility to save it.

The problem isn't that the variable is not set, when someone clicks on the Button (which makes a value auto-fill), the problem is that the Button causes the page to be reloaded and the var value is lost. And i need to keep the value until the user tries to leave the page.

REFLECTION

Perhaps I could set cookies across the page reloads ...

Upvotes: 0

Views: 987

Answers (4)

Chicharito
Chicharito

Reputation: 1440

Try:

$('[id$=myTextBox]').change();

Upvotes: 0

David Morton
David Morton

Reputation: 16505

Seems like you should be setting this other variable, the one that indicates whether the value has changed or not, from the code-behind when the value changes. In other words, it looks like you need to do this in two places.

Alternatively, you could change the button to set the value via javascript, and set it's OnClientClick handler instead of it's OnClick handler. Make sure the end of the OnClientClick returns false, to avoid the postback. In that case, you'd be able to avoid the postback altogether, and your problem would be solved.

<asp:Button runat="server" ID="someId" OnClientClick="$('#myTextBox').val('someValue');return false;"/>

Upvotes: 0

Rune
Rune

Reputation: 8380

If I understand your situation correctly, you know server-side if the value has changed. Then just make sure that you output

$('#myTextBox').change();

when this is the case. You could put this code inside a placeholder and in the button event handler toggle a bool determining whether the placeholder is rendered or not.

Upvotes: 1

Nick Craver
Nick Craver

Reputation: 630587

You probably need to adjust the ID if it's inside anything:

$("#<%=myTextBox.ClientID %>").change(function() {});

Upvotes: 1

Related Questions