Arikkan
asked on
Server Postback called from JavaScript does not call the code behind
I am facing this issue:
1. I have a webpage that is calling Javascript function on page_load(). It is a recursive function and enables a button on the page on getting valid value from a Cookie.
2. Right now the Enabled 'FINISH' button is clicked by the user manually. This calls the code behind and runs all functionality as needed.
3. The client wants me to run that functionality automatically now.
4. So, now I want that the Enabled 'FINISH' button to be clicked automatically which will run server side code for button click and add a record in the database based on that.
5. Somehow my code for the button click is NOT calling the CODE BEHIND after calling the click event in JavaScript.
6. I debugged and the below code is running without an error. But no call to server side event !!!!
var btnFinishOnline = document.getElementById(bt nFinish);
if(condition == 'true')
{
btnFinishOnline.click();
}
Any ideas?
1. I have a webpage that is calling Javascript function on page_load(). It is a recursive function and enables a button on the page on getting valid value from a Cookie.
2. Right now the Enabled 'FINISH' button is clicked by the user manually. This calls the code behind and runs all functionality as needed.
3. The client wants me to run that functionality automatically now.
4. So, now I want that the Enabled 'FINISH' button to be clicked automatically which will run server side code for button click and add a record in the database based on that.
5. Somehow my code for the button click is NOT calling the CODE BEHIND after calling the click event in JavaScript.
6. I debugged and the below code is running without an error. But no call to server side event !!!!
var btnFinishOnline = document.getElementById(bt
if(condition == 'true')
{
btnFinishOnline.click();
}
Any ideas?
ASKER
I tried it and got this message:
"JavaScript runtime error: Object doesn't support property or method 'replace'"
"JavaScript runtime error: Object doesn't support property or method 'replace'"
Where did you get that error?
ASKER
I got it on __doPostBack(...) statement as below:
var btnFinishOnline = document.getElementById(bt nFinish);
if (condition == true)
{
__doPostBack(btnFinishOnli ne, '');
}
var btnFinishOnline = document.getElementById(bt
if (condition == true)
{
__doPostBack(btnFinishOnli
}
1) What browser and version are you using?
2) Are you able to debug JavaScript?
2) Are you able to debug JavaScript?
ASKER
1. Here I am using IE (10.0.9200.17267)
2. I am able to debug JavaScript
2. I am able to debug JavaScript
I need to know what object it is trying to call "replace" method on. It would be nice if there was an error condition shown, or if you could attach a debugger, and step through the code.
ASKER
When I debug, this is what I get (Bolded lines are the cause of the error):
The error is thrown at the second function that is called from the first.
Function.createDelegate = function Function$createDelegate(in stance, method) {
/// <summary locid="M:J#Function.create Delegate" />
/// <param name="instance" mayBeNull="true"></param>
/// <param name="method" type="Function"></param>
/// <returns type="Function"></returns>
var e = Function._validateParams(a rguments, [
{name: "instance", mayBeNull: true},
{name: "method", type: Function}
]);
if (e) throw e;
return function() {
return method.apply(instance, arguments);
}
}
function Sys$WebForms$PageRequestMa nager$_uni queIDToCli entID(uniq ueID) {
return uniqueID.replace(/\$/g, '_');
}
The error is thrown at the second function that is called from the first.
Function.createDelegate = function Function$createDelegate(in
/// <summary locid="M:J#Function.create
/// <param name="instance" mayBeNull="true"></param>
/// <param name="method" type="Function"></param>
/// <returns type="Function"></returns>
var e = Function._validateParams(a
{name: "instance", mayBeNull: true},
{name: "method", type: Function}
]);
if (e) throw e;
return function() {
return method.apply(instance, arguments);
}
}
function Sys$WebForms$PageRequestMa
return uniqueID.replace(/\$/g, '_');
}
How is "btnFinish" defined in the HTML?
ASKER
<%@ Page Language="VB" AutoEventWireup="false"... .......... .......... ...... %>
<%@ Register .......................... ... %>
<asp:Content ID="contentHead" ContentPlaceHolderID="Head " runat="server">
</asp:Content>
<asp:Content ID="content" ContentPlaceHolderID="Body Content" Runat="Server">
<asp:Label ID="lblPageHeading" runat="server" CssClass="PageHeading"></a sp:Label>
<div class="ValidationSummary">
.....
.....
.....
</div>
<asp:Wizard ID="wizCandidate" runat="server" DisplaySideBar="false" ActiveStepIndex="0">
<StartNavigationTemplate>
<asp:Button ID="btnNext" runat="server" CommandName="MoveNext" Text="Next" />
</StartNavigationTemplate>
<StepNavigationTemplate>
<asp:Button ID="btnPrevious" runat="server" CausesValidation="False" CommandName="MovePrevious"
Text="Previous" />
<asp:Button ID="btnNext" runat="server" CommandName="MoveNext" Text="Next" />
</StepNavigationTemplate>
<FinishNavigationTemplate>
<table border=0 width="100%">
<tr >
<td align=left>   60;<asp:Bu tton ID="btnPay" runat="server" Text='<%#Resources.Resourc e.Candidat eBTNPayOnl ine%>'/></ td><td align=right> <asp:Button ID="StepPreviousButton" runat="server" CausesValidation="False" CommandName="MovePrevious"
Text="Previous" />
<asp:Button ID="btnFinish" runat="server" Text="Finish" CausesValidation="true" CommandName="MoveComplete" /></td>
<asp:HiddenField ID="hiddenFieldbtnFinishCl icked" runat="server" Visible="true" Value="0" />
</tr>
</table>
</FinishNavigationTemplate >
<WizardSteps>
.....
.....
.....
</WizardSteps>
</asp:Wizard>
</asp:Content>
<%@ Register ..........................
<asp:Content ID="contentHead" ContentPlaceHolderID="Head
</asp:Content>
<asp:Content ID="content" ContentPlaceHolderID="Body
<asp:Label ID="lblPageHeading" runat="server" CssClass="PageHeading"></a
<div class="ValidationSummary">
.....
.....
.....
</div>
<asp:Wizard ID="wizCandidate" runat="server" DisplaySideBar="false" ActiveStepIndex="0">
<StartNavigationTemplate>
<asp:Button ID="btnNext" runat="server" CommandName="MoveNext" Text="Next" />
</StartNavigationTemplate>
<StepNavigationTemplate>
<asp:Button ID="btnPrevious" runat="server" CausesValidation="False" CommandName="MovePrevious"
Text="Previous" />
<asp:Button ID="btnNext" runat="server" CommandName="MoveNext" Text="Next" />
</StepNavigationTemplate>
<FinishNavigationTemplate>
<table border=0 width="100%">
<tr >
<td align=left>  
Text="Previous" />
<asp:Button ID="btnFinish" runat="server" Text="Finish" CausesValidation="true" CommandName="MoveComplete"
<asp:HiddenField ID="hiddenFieldbtnFinishCl
</tr>
</table>
</FinishNavigationTemplate
<WizardSteps>
.....
.....
.....
</WizardSteps>
</asp:Wizard>
</asp:Content>
Sorry, I meant to say the rendered output (View Source in browser). I wanted to see how the unique ID/client ID was generated.
ASKER
Here is the view source from the page:
<table border="0" width="100%">
<tbody>
<tr>
<td align="left"> & nbsp;
<input type="submit" name="ctl00$BodyContent$wi zCandidate $FinishNav igationTem plateConta inerID$btn Pay" value="Start Payment Process" onclick="return ShowModalDialogPage('Payme ntForm.asp x');" id="ctl00_BodyContent_wizC andidate_F inishNavig ationTempl ateContain erID_btnPa y" disabled="">
</td>
<td align="right">
<input type="submit" name="ctl00$BodyContent$wi zCandidate $FinishNav igationTem plateConta inerID$Ste pPreviousB utton" value="Previous" id="ctl00_BodyContent_wizC andidate_F inishNavig ationTempl ateContain erID_StepP reviousBut ton" disabled="">
<input type="submit" name="ctl00$BodyContent$wi zCandidate $FinishNav igationTem plateConta inerID$btnFinish" value="Finish" id="ctl00_BodyContent_wizC andidate_F inishNavig ationTempl ateContain erID_btnFi nish">
</td>
<input type="hidden" name="ctl00$BodyContent$wi zCandidate $FinishNav igationTem plateConta inerID$hid denFieldbt nFinishCli cked" id="ctl00_BodyContent_wizC andidate_F inishNavig ationTempl ateContain erID_hidde nFieldbtnF inishClick ed" value="0">
</tr>
</tbody>
</table>
<table border="0" width="100%">
<tbody>
<tr>
<td align="left"> &
<input type="submit" name="ctl00$BodyContent$wi
</td>
<td align="right">
<input type="submit" name="ctl00$BodyContent$wi
<input type="submit" name="ctl00$BodyContent$wi
</td>
<input type="hidden" name="ctl00$BodyContent$wi
</tr>
</tbody>
</table>
I don't see the btnFinishOnline in that output, but if it was btnFinish, you could try this as a test:
__doPostBack('ctl00_BodyCo ntent_wizC andidate_F inishNavig ationTempl ateContain erID_btnFi nish');
__doPostBack('ctl00_BodyCo
ASKER
Hi,
Thank you. I will try this and get back to you.
"btnFinishOnline" was just the variable name in JavaScript function. The control name is "btnFinish"
var btnFinishOnline = document.getElementById(bt nFinish);
Thank you. I will try this and get back to you.
"btnFinishOnline" was just the variable name in JavaScript function. The control name is "btnFinish"
var btnFinishOnline = document.getElementById(bt
ASKER
Ok. Now I can see that the code did not throw any error. But somehow the code-behind was never hit.
I had a breakpoint there.
Also I double checked and no value was inserted in the DB. So looks like the code behind did not get called.
I had a breakpoint there.
Also I double checked and no value was inserted in the DB. So looks like the code behind did not get called.
If you put a break-point in the page load event handler, does that get hit?
Does btnFinish.UniqueID return a value?
ASKER
Oh. The Page Load event handler does get called. So strange.
Does btnFinish.UniqueID return a value? --> YES
btnFinish.UniqueID = "ms__id1"
Does btnFinish.UniqueID return a value? --> YES
btnFinish.UniqueID = "ms__id1"
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Tried it. The code ran without any errors and Page_Load() was called surely.
But the code behind for the Button_Click was not called.
:-(
What am I missing?
But the code behind for the Button_Click was not called.
:-(
What am I missing?
ASKER
ok. I used this:
__doPostBack('ms__id1', 'btnFinish_Click__JavaScri pt');
I can access the passed PARAMETER in PAGE_LOAD()
Dim passedArgument As String = Request.Params.Get("__EVEN TARGUMENT" )
If passedArgument = "btnFinish_Click__JavaScri pt" Then
FinishButtonClick(sender, Nothing)
End If
But then, I have this:
There is a RaiseEvent() that is not firing. Perhaps the sender parameter is wrong.
Any ideas?
Protected Sub FinishButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls. WizardNavi gationEven tArgs) Handles wizCandidate.FinishButtonC lick
....
....
....
RaiseEvent CandidateFilingFinishButto nClick(sen der, e) //Now this won't fire ????
....
....
....
....
End Sub
__doPostBack('ms__id1', 'btnFinish_Click__JavaScri
I can access the passed PARAMETER in PAGE_LOAD()
Dim passedArgument As String = Request.Params.Get("__EVEN
If passedArgument = "btnFinish_Click__JavaScri
FinishButtonClick(sender, Nothing)
End If
But then, I have this:
There is a RaiseEvent() that is not firing. Perhaps the sender parameter is wrong.
Any ideas?
Protected Sub FinishButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.
....
....
....
RaiseEvent CandidateFilingFinishButto
....
....
....
....
End Sub
What does this do?
RaiseEvent CandidateFilingFinishButto nClick(sen der, e)
RaiseEvent CandidateFilingFinishButto
ASKER
It implements an EventHandler in the page View.
The page inherits from a view.
Partial Public Class CandidateFormation
Inherits BasePage
Implements ICandidateFormationView
....
....
....
....
Private Event CandidateFilingFinishButto nClick As EventHandler Implements ICandidateFormationView._c andidateFi lingFinish ButtonClic k
....
....
....
....
Protected Sub wizCandidate_FinishButtonC lick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls. WizardNavi gationEven tArgs) Handles wizCandidate.FinishButtonC lick
....
....
....
RaiseEvent CandidateFilingFinishButto nClick(sen der, e)
....
....
....
End Sub
End Class
The page inherits from a view.
Partial Public Class CandidateFormation
Inherits BasePage
Implements ICandidateFormationView
....
....
....
....
Private Event CandidateFilingFinishButto
....
....
....
....
Protected Sub wizCandidate_FinishButtonC
....
....
....
RaiseEvent CandidateFilingFinishButto
....
....
....
End Sub
End Class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok I will try and get back to you. Thanks.
ASKER
I called the "FinishButtonClick(....)" in "PAGE_LOAD(....)
ASKER
Thanks a lot Bob. :-)
btnFinishOnline.click();
You could use the __doPostBack Javascript function:
_doPostBack function in javascript - asp.net postback mechanism
http://dotnetprof.blogspot.com/2012/08/dopostback-function-in-javascript.html