最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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) 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?

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) 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?

Share Improve this question edited Nov 16, 2011 at 21:10 Joel Coehoorn 417k114 gold badges578 silver badges815 bronze badges asked Sep 12, 2011 at 13:37 MrJDMrJD 1,8891 gold badge23 silver badges40 bronze badges 3
  • Why you can't fit a Javascript function? – Guilherme de Jesus Santos Commented Sep 12, 2011 at 13:48
  • So what exactly is the problem? Are the values not changing... does your function not run... a little more details into the exact problem you are trying to solve would be helpful. – Josh Commented Sep 12, 2011 at 13:54
  • I edited my question for you, the function indeed runs and changes the values client side, however it doesn't translate to the server side, because server side fired first. – MrJD Commented Sep 13, 2011 at 6:31
Add a ment  | 

5 Answers 5

Reset to default 2

Yup, this function should work

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

But one culprit might be the mented 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');

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

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.

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

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

http://jsfiddle/sreeprasad/7urvg/

发布评论

评论列表(0)

  1. 暂无评论