Here is the code generated by asp.net on the onclick event: onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$mainContent$buttons$btnUpdate",
"", true, "", "", false, false))" Here is what I want to try to achieve:
a. On the onclick event class a web page method
b. Upon reception of the web page method response and depending on the result, I want to the form to submit or do something else. Here
is some code (the web page method just return true). The
The javascript in the page is as follow:<script type="text/javascript" language="javascript">function ISOK()
{PageMethods.ISOK(OnSucceededISOK, OnFailedISOK);}
// Callback function invoked on successful completion of the page method.function OnSucceededISOK(result, userContext, methodName)
{if (result == true){// This works__doPostBack('ctl00$mainContent$buttons$btnUpdate', '');
// This does not work. Javascript error when loading the pageWebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$mainContent$buttons$btnUpdate", "", true, "", "", false, false));
// This does not work. The form is not submitted, no error reportedWebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00_mainContent_buttons_btnUpdate", "", true, "", "", false, false));
}
else{alert('error 1');
}
// Callback function invoked on failure of the page method.
function OnFailedISOK(error, userContext, methodName)
{if (error !== null) {
So on OnSucceededISOK, if I use __doPostBack the form is submitted. No problem here. However, I know that this method is depreciated and the one to use is WebForm_DoPostBackWithOptions, but I cannot get
it to work. It gives me a javascript error when loading the page if I just use the code generated in the first place. Or it just does not submit the form and does not report any error if a replace "e; with " How
can I call WebForm_DoPostBackWithOptions from javascript?I did not find any relevant documentation on this method. Thank you
Here is a sample. When you click on the Submit button, the text label of the label is updated with the current date.
1.
With __doPostBack('btnSubmit',''); I can see the alert triggered, the form is posted, and the date is refreshed
2.
With WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("btnSubmit", "", true, "", "", false, false));. This is waht is generated by ASP.Net. There is an error on the page. The javascript function fn() is not
executed as I don't see the alert. However, the original WebForm_DoPostBackWithOptions is executed as the date is refreshed
3.
With WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('btnSubmit', '', true, '', '', false, false)); The javascript function is executed as alert is triggered, but the following line is not executed. The form is not submitred as the date is not
refreshed. No error is reported
Also, it took me a while to figure out that asp.net generates the following: WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("btnSubmit", "", true, "", "", false, false)); on the onclick event only when
there is a validator in the page. This is why there is a required validator in my page sample.
The bolded option indicates that the option object's clientSubmit property is false. And in the definition of WebForm_DoPostBackWithOptions, you'll find the following line:
if (options.clientSubmit) {
<div class="sourceRow">__doPostBack(options.eventTarget, options.eventArgument);</div><div class="sourceRow">}</div><div class="sourceRow"> </div><div class="sourceRow">That's to say, when clientSubmit is false, it doesn't call the __doPostBack function to post current page. So, you need to specify it to
true. </div><div class="sourceRow"> </div><div class="sourceRow">Then, you might ask, why it behaves correctly with the auto-generated script. The key is that the button control is rendered as an input element of type
"Submit" , thus it doesn't have to call the __doPostBack to submit current page.</div><div class="sourceRow"> </div><div class="sourceRow">Then, why the submit doesn't take place in your sample since is rendered as a submit element too? That's because
you specify "return false; " in your code.</div><div class="sourceRow">This statement blocks the following actions after it to take place.</div><div> </div><div> </div><div>This is the full story. Hope I illustrate myself clearly enough. Please feel
free to let me know if there is any problem. </div>
Didier Marin
Member
2 Points
10 Posts
Problem with calling WebForm_DoPostBackWithOptions from javascript
Jul 06, 2007 08:51 PM|LINK
a. On the onclick event class a web page method
b. Upon reception of the web page method response and depending on the result, I want to the form to submit or do something else. Here is some code (the web page method just return true). Theclick event becomes:
onclick=" ISOK(); return false; WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$mainContent$buttons$btnUpdate", "", true, "", "", false, false))"
The javascript in the page is as follow:<script type="text/javascript" language="javascript">function ISOK() { PageMethods.ISOK(OnSucceededISOK, OnFailedISOK);}
// Callback function invoked on successful completion of the page method.function OnSucceededISOK(result, userContext, methodName) { if (result == true) { // This works __doPostBack('ctl00$mainContent$buttons$btnUpdate', ''); // This does not work. Javascript error when loading the pageWebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$mainContent$buttons$btnUpdate", "", true, "", "", false, false)); // This does not work. The form is not submitted, no error reportedWebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00_mainContent_buttons_btnUpdate", "", true, "", "", false, false));}
else { alert('error 1');}
// Callback function invoked on failure of the page method.
function OnFailedISOK(error, userContext, methodName) { if (error !== null) {alert('error 2');
}
} if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();</script>
So on OnSucceededISOK, if I use __doPostBack the form is submitted. No problem here. However, I know that this method is depreciated and the one to use is WebForm_DoPostBackWithOptions, but I cannot get it to work. It gives me a javascript error when loading the page if I just use the code generated in the first place. Or it just does not submit the form and does not report any error if a replace "e; with " How can I call WebForm_DoPostBackWithOptions from javascript?I did not find any relevant documentation on this method. Thank you
DidierRaymond Wen ...
All-Star
32101 Points
3764 Posts
Re: Problem with calling WebForm_DoPostBackWithOptions from javascript
Jul 11, 2007 07:30 AM|LINK
Hi Didier,
You've already given a good explanation of your problem and I had a general idea now.
But it will be easier for us to try to solve it if you can provide a simple sample, can you?
Didier Marin
Member
2 Points
10 Posts
Re: Problem with calling WebForm_DoPostBackWithOptions from javascript
Jul 11, 2007 06:55 PM|LINK
No problem Raymond,
Here is a sample. When you click on the Submit button, the text label of the label is updated with the current date.
1.
With __doPostBack('btnSubmit',''); I can see the alert triggered, the form is posted, and the date is refreshed
2.
With WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("btnSubmit", "", true, "", "", false, false));. This is waht is generated by ASP.Net. There is an error on the page. The javascript function fn() is not executed as I don't see the alert. However, the original WebForm_DoPostBackWithOptions is executed as the date is refreshed
3.
With WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('btnSubmit', '', true, '', '', false, false)); The javascript function is executed as alert is triggered, but the following line is not executed. The form is not submitred as the date is not refreshed. No error is reported
Also, it took me a while to figure out that asp.net generates the following: WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("btnSubmit", "", true, "", "", false, false)); on the onclick event only when there is a validator in the page. This is why there is a required validator in my page sample.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
<html>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="scriptManager" runat="server" />
<script type="text/javascript" language="javascript">
function fn()
{
alert(1);
__doPostBack('btnSubmit','');
// WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("btnSubmit", "", true, "", "", false, false));
//WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('btnSubmit', '', true, '', '', false, false));
}
</script>
<asp:updatepanel id="updatePanel" runat="server">
<contenttemplate>
<div id="div" runat="server">
<asp:textbox id="txtName" runat="server" maxlength="64" cssclass="inputText" width="310"></asp:textbox>
<asp:requiredfieldvalidator id="valrName" runat="server" errormessage="NameRequired"
display="Dynamic" controltovalidate="txtName">*</asp:requiredfieldvalidator>
<asp:button id="btnSubmit" runat="server" text="Submit" onclick="btnSubmit_Click" /><br /><br />
<asp:label id="lbl1" runat="server" />
</div>
</contenttemplate>
</asp:updatepanel>
</form>
</body>
</html>
using System;
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
btnSubmit.OnClientClick = "fn();return false;";
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
lbl1.Text = System.DateTime.Now.ToString();
}
}
Raymond Wen ...
All-Star
32101 Points
3764 Posts
Re: Problem with calling WebForm_DoPostBackWithOptions from javascript
Jul 12, 2007 01:55 AM|LINK
Hi,
The reason with situation 3 is that you didn't specify a correct option in the script function fn().
<script type="text/javascript" language="javascript">
function fn()
{
WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('btnSubmit', '', true, '', '', false, false));
}
</script>
The bolded option indicates that the option object's clientSubmit property is false. And in the definition of WebForm_DoPostBackWithOptions, you'll find the following line:
if (options.clientSubmit) {
<div class="sourceRow"> __doPostBack(options.eventTarget, options.eventArgument);</div><div class="sourceRow"> }</div><div class="sourceRow"> </div><div class="sourceRow">That's to say, when clientSubmit is false, it doesn't call the __doPostBack function to post current page. So, you need to specify it to true. </div><div class="sourceRow"> </div><div class="sourceRow">Then, you might ask, why it behaves correctly with the auto-generated script. The key is that the button control is rendered as an input element of type "Submit" , thus it doesn't have to call the __doPostBack to submit current page.</div><div class="sourceRow"> </div><div class="sourceRow">Then, why the submit doesn't take place in your sample since is rendered as a submit element too? That's because you specify "return false; " in your code.</div><div class="sourceRow">This statement blocks the following actions after it to take place.</div><div> </div><div> </div><div>This is the full story. Hope I illustrate myself clearly enough. Please feel free to let me know if there is any problem. </div>Didier Marin
Member
2 Points
10 Posts
Re: Problem with calling WebForm_DoPostBackWithOptions from javascript
Jul 13, 2007 02:30 PM|LINK
Thank you Raymond,
That clarifies things a lot. I was not able to find any documentation about the parameters for WebForm_PostBackOptions.
Thank you again
Didier
JByrd2007
Member
447 Points
93 Posts
Re: Problem with calling WebForm_DoPostBackWithOptions from javascript
Jul 25, 2007 03:11 PM|LINK
The "WebForm_" prefix is prepended when the form is rendered. You can see MSDN help for PostBackOptions here:
http://msdn2.microsoft.com/en-us/library/system.web.ui.postbackoptions(vs.80).aspx
Jon
Jon
If I was able to help, please mark this post as Answer.
udayshelar
Member
2 Points
1 Post
Re: Problem with calling WebForm_DoPostBackWithOptions from javascript
Jun 02, 2008 05:21 PM|LINK
Hi Raymond,
I am stucking with the WebForm_PostBackOptions.
I have one web form on which i have been placed the user control. when I click to button from user control it shows the Ajax pop-up control.
after that when I click to save button on popup control it saves Data and get back to the page and call the one other button control..
upto this everything is working fine.
Now I have a situation i need to call the Javascript function instead of the button Onclick event.
I need to get call to OnClientClick event, am stucked with this and not getting how to call it...
Please help me.
Thanks,
Uday.
.NET 2.0 .ASP.Net 2.0 "ASP.NET AJAX Beta 2"
aleivaal
Member
4 Points
4 Posts
Re: Problem with calling WebForm_DoPostBackWithOptions from javascript
Nov 12, 2008 07:15 PM|LINK
Thanks both, I had the same problem and I could fix it with your comments...