开发者

ASP.NET jquery function call

开发者 https://www.devze.com 2022-12-21 19:08 出处:网络
Suppose an ASP.NET .NET 3.5 Site. There is an asp:TextBox defined with this jquery javascript attached to it:

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.

REFLEC开发者_Go百科TION

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


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

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


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.


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;"/>


Try:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号