MrJD
MrJD

Reputation: 1889

Change value of text box using javascript (Jquery) before post back ASP.NET

I am trying to change the value of a set of text boxes (that hold greyed out suggestions in them) when the user presses the submit form button. This button runs a server side method using OnClientClick that submits the data and does a whole slew of other things.

Now my problem is that i can't either: fit in a javascript function that will change the values before the server gets hit, OR call the server side method in the javascript instead of the button OnClientClick event.

Ive tried:

$(this.form).submit(function(){
//Change value here
});
//using OnClientClick to call function

and

$("#"+"<%=submitBtn.ClientID %>").click(function(){
    //Change values here
    __doPostBack("<%=submitBtn.ClientID %>");
});
//not using OnClientClick to call server method

(pretty sure that won't work) and

$("#"+"<%=submitBtn.ClientID %>").click(function(){
    //Change values here
});
//using OnClientClick still to call function

Im stumped


Edit

Right I obviously didn't give enough info, What happens when I use submit events is the server event fires before the JavaScript event, therefore when do a server side validation before I send the values away, I have the wrong values, there isn't any point in changing to client side validation because i will still have the same problem when I send the form data back to the db.


Update

So i still have a problem (both with this and mentally because of this).
Because of the idiots who worked on this before me (now i have to fix it) they removed the submit behaviour (asp.net) from the button at the bottom, because they use some server side trickery to figure out if some validators should be on or off (when really it should be client side that does that), hence they had to turn it off because it would fire validators if it didn't.

ANYWAY... So I'm still having trouble, the on click function for the button doesn't seem to fire in time or the scripts run simultaneously. I tested this by adding an alert and a breakpoint on the code behind, the breakpoint fires and the alert fires too. sooo..... yeah. Is there any way i could maybe circumvent this by removing the "onclientclick" from the button and calling the function it calls in the CB? Any ideas? (Please?)


Small update:

Still can't figure it out :(. Is anyone confused by the question?

Upvotes: 1

Views: 7318

Answers (5)

Scott
Scott

Reputation: 11

Just stumbled across this. It's been a month since you first posted the question so you may have fixed it already. However, thought I'd add my thoughts anyway.

First, it's make sense your code is being ignored. When you bind to the submit and onclick events you functions are added to the list of events handles. Events handlers are processed from the first added to the last added. So the postback is started before your jquery code is called.

To solve this you need to change the code in the function that OnClientClick calls or write a wrapper function that calls your code then calls what OnClientClick called and change OnClientClick to call your wrapper function.

Upvotes: 1

Leon
Leon

Reputation: 3401

The form is submitted before JS fires all events.

Have your button's onClientClick event change the values. You can call functions in sequence if you need to.

jQuery does some funky things with events and you can't be sure what order they will fire after they are attached. You must explicitly specify the functions to call.

<button id="some_button" onclick="SetValues(); SubmitForm();" />

Upvotes: 1

sreeprasad
sreeprasad

Reputation: 3260

I have used id of submit button to check for a "click"

http://jsfiddle.net/sreeprasad/7urvg/

Upvotes: 0

mattsahr
mattsahr

Reputation: 528

Yup, this function should work

$(this.form).submit(function(){
//Change value here
});

But one culprit might be the commented part: "// Change values here." If you're using one of these,

$('#target').text('my new info');
$('#target').html('my new info');

...you will have trouble. You need to use .val()

$('#target').val('my new info');

Upvotes: 2

clem
clem

Reputation: 3366

this function should do the trick:

$(this.form).submit(function(){
//Change value here
});

just make sure you enable the text fields again before you send them to the server, otherwise the server won't pick them up

Upvotes: 0

Related Questions