Postback from javascript

M

Martin Eyles

I am trying to get javascript to cause a page to post back. I have tried
calling _doPostBack from my script, but generates an error "object
expected". I think this is because the page's script doesn't contain the
method _doPostBack, which needs to be added by asp.net. How can I make
asp.net add this script?

Thanks,
ME
 
J

john morales

Hello Martin,

I believe asp.net automatically adds this as soon as you add a linkbutton
or some other control that can cause a postback. Regular Button controls
would not trigger asp.net framework to add the javascript postback code since
html by default supports posting the form without javascript intervention.

I'm not sure how to trick asp.net into adding this code except for adding
a dummy linkbutton and not setting the text property, which would make the
linkbutton not take any space or be invisible to the user...

i'm sure there are better ways to accomplish this as what i'm suggesting
is a work around.


-john
 
B

bruce barker

Page.GetPostBackEventReference(this);

will trigger generation of the javascript postback code. you might want to
write a control that supports client postbacks.

-- bruce (sqlwork.com)





| I am trying to get javascript to cause a page to post back. I have tried
| calling _doPostBack from my script, but generates an error "object
| expected". I think this is because the page's script doesn't contain the
| method _doPostBack, which needs to be added by asp.net. How can I make
| asp.net add this script?
|
| Thanks,
| ME
|
| --
| Martin Eyles
| (e-mail address removed)
|
|
 
K

Kevin Spencer

Just submit the form.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.
 
M

Martin Eyles

I added a submit button, but this has not added the javascript method. It
did add an action of the form, pointing to the current page. Also, the
editor picked up the button was submit and added a value to the button
("Submit Query").

Any more ideas, Thanks, ME
 
M

Martin Eyles

Page.GetPostBackEventReference(this); made an error in the task list (this
not declared). I changed this to the name of an input on the page, and the
error disappeared. Still, it didn't solve my problem.

Thanks anyway, ME
 
M

Martin Eyles

I thought calling _doPostBack was how you submitted an asp.net form. Is
there something else I can call from javascript that will submit the form -
maybe using the DOM? (I have not posted a form from javascript before).

Thanks,
ME
 
W

William F. Robertson, Jr.

javascript:

document.forms[0].submit();

That will submit the form back to the server.

bill
 
K

Kumar Reddi

Yeah, as William suggest, if you simply want to postback using javascript,
use document.forms[0].submit();. Or if you want to find out which control
caused the postback or to postback using a tradional non postback control,
then you can use the __doPostBack(). You do not have to make asp.net
generate the code, you can simply paste the following code. Thats exactly
what asp.net generate for you if you put linkbutton or whatever

<!--

function __doPostBack(eventTarget, eventArgument) {
var theform;
if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
theform = document.forms["Form1"];
}
else {
theform = document.Form1;
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
alert(theform.__EVENTARGUMENT.value);
theform.submit();
}
// -->

Also, for this code to work, you need to place the following two hidden html
controls on your form

<input type="hidden" name="__EVENTTARGET" />
<input type="hidden" name="__EVENTARGUMENT" />
 
K

Kevin Spencer

A submit button submits a form. It doesn't need any JavaScript.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.
 
J

john morales

yes i'm aware of that, i mentioned it in my earlier post, the reason i suggested
to use a linkbutton was specifically that it doesnt support submitting the
form by default, thus asp.net would add the postback javascript block.

The question was "how do i get asp.net to add this script" not "how do i
submit the form"
 
K

Kevin Spencer

The question was "how do i get asp.net to add this script" not "how do i
submit the form"

The question was how to create a PostBack. I suggested simply submitting the
form. You said that you added a submit button. A submit button submits a
form. No JavaScript necessary.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.
 
M

Martin Eyles

Thanks,
In the end, I modified this slightly to get
document.getElementById("Form1").submit();
This works beautifully, posting back the form.
Just one other thing. If I use IsPostBack on the server side, will this be
detected as such? If not, what do I need to do?

ME

--
Martin Eyles
(e-mail address removed)

William F. Robertson said:
javascript:

document.forms[0].submit();

That will submit the form back to the server.

bill
 
M

Martin Eyles

Thanks guys, this has given me some insight into how the whole postback for
<asp:anything> items works, and how the objects can all be identified server
side. I took the suggestion from William in the end and did

document.getElementById("Form1").submit();

which does exactly what I want.

Thanks anyway,
ME
 
M

Martin Eyles

Half answered my own question. the form submition does not get detected as a
post-back by the server. What am I doing wrong?

--
Martin Eyles
(e-mail address removed)

Martin Eyles said:
In the end, I modified this slightly to get
document.getElementById("Form1").submit();
This works beautifully, posting back the form.
Just one other thing. If I use IsPostBack on the server side, will this
be detected as such? If not, what do I need to do?

William F. Robertson said:
javascript:document.forms[0].submit();

That will submit the form back to the server.

Martin Eyles said:
I thought calling _doPostBack was how you submitted an asp.net
form. Is there something else I can call from javascript that will
submit the form - maybe using the DOM? (I have not posted a
form from javascript before).

Just submit the form.

I am trying to get javascript to cause a page to post back
 
J

john morales

Hello Martin,

try using inserting the javascript code Kumar suggested and calling the __doPostBack
javascript function.

-john
Half answered my own question. the form submition does not get
detected as a post-back by the server. What am I doing wrong?

Martin Eyles said:
In the end, I modified this slightly to get
document.getElementById("Form1").submit();
This works beautifully, posting back the form.
Just one other thing. If I use IsPostBack on the server side, will
this
be detected as such? If not, what do I need to do?
William F. Robertson said:
javascript:document.forms[0].submit();

That will submit the form back to the server.


I thought calling _doPostBack was how you submitted an asp.net
form. Is there something else I can call from javascript that will
submit the form - maybe using the DOM? (I have not posted a
form from javascript before).

Just submit the form.


I am trying to get javascript to cause a page to post back
 
M

Martin Eyles

what should I put in the eventTarget and eventArgument variables? I can't
use a part of the form, as the postback is coming from a script (called from
another window), not the form. Note, I am not too bothered about working out
what caused post back, just that postback has occured.

ME
 
W

William F. Robertson, Jr.

If you look carefully at the __doPostBack method that asp.net uses to post
back the page, you will see the functions sets two hidden text fields. The
hidden text fields are used to notify the processing page what control
caused the postback (for say a button click) and any parameters passed by
it.

Since you requirement is only a post back occured not caring about what
caused the postback, you can just call the submit method on the form object.

document.forms[0].submit();

This is the same method Microsoft uses to submit the form, and it will
accomplish what you are trying to accomplish with minimal effort.

bill

Martin Eyles said:
what should I put in the eventTarget and eventArgument variables? I can't
use a part of the form, as the postback is coming from a script (called from
another window), not the form. Note, I am not too bothered about working out
what caused post back, just that postback has occured.

ME

--
Martin Eyles
(e-mail address removed)

Kumar Reddi said:
Yeah, as William suggest, if you simply want to postback using javascript,
use document.forms[0].submit();. Or if you want to find out which control
caused the postback or to postback using a tradional non postback control,
then you can use the __doPostBack(). You do not have to make asp.net
generate the code, you can simply paste the following code. Thats exactly
what asp.net generate for you if you put linkbutton or whatever

<!--

function __doPostBack(eventTarget, eventArgument) {
var theform;
if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
theform = document.forms["Form1"];
}
else {
theform = document.Form1;
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
alert(theform.__EVENTARGUMENT.value);
theform.submit();
}
// -->

Also, for this code to work, you need to place the following two hidden html
controls on your form

<input type="hidden" name="__EVENTTARGET" />
<input type="hidden" name="__EVENTARGUMENT" />
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top