Link to home
Start Free TrialLog in
Avatar of Arikkan
ArikkanFlag for United States of America

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(btnFinish);

if(condition == 'true')
{
    btnFinishOnline.click();
}


Any ideas?
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

This is a client-side event, and not a server-side event, so no postback:

      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
Avatar of Arikkan

ASKER

I tried it and got this message:

"JavaScript runtime error: Object doesn't support property or method 'replace'"
Where did you get that error?
Avatar of Arikkan

ASKER

I got it on __doPostBack(...) statement as below:

var btnFinishOnline = document.getElementById(btnFinish);
if (condition ==  true)
{                        
    __doPostBack(btnFinishOnline, '');
}
1) What browser and version are you using?

2) Are you able to debug JavaScript?
Avatar of Arikkan

ASKER

1. Here I am using IE (10.0.9200.17267)
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.
Avatar of Arikkan

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(instance, method) {
    /// <summary locid="M:J#Function.createDelegate" />
    /// <param name="instance" mayBeNull="true"></param>
    /// <param name="method" type="Function"></param>
    /// <returns type="Function"></returns>
    var e = Function._validateParams(arguments, [
        {name: "instance", mayBeNull: true},
        {name: "method", type: Function}
    ]);
    if (e) throw e;
    return function() {
        return method.apply(instance, arguments);
    }
}


    function Sys$WebForms$PageRequestManager$_uniqueIDToClientID(uniqueID) {
        return uniqueID.replace(/\$/g, '_');
    }
How is "btnFinish" defined in the HTML?
Avatar of Arikkan

ASKER

<%@ Page Language="VB" AutoEventWireup="false"............................. %>
<%@ Register ............................. %>

<asp:Content ID="contentHead" ContentPlaceHolderID="Head" runat="server">
   
</asp:Content>

<asp:Content ID="content" ContentPlaceHolderID="BodyContent" Runat="Server">
            <asp:Label ID="lblPageHeading" runat="server" CssClass="PageHeading"></asp: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>&#160;&#160;&#160;<asp:Button ID="btnPay" runat="server" Text='<%#Resources.Resource.CandidateBTNPayOnline%>'/></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="hiddenFieldbtnFinishClicked" runat="server" Visible="true" Value="0" />
                </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.
Avatar of Arikkan

ASKER

Here is the view source from the page:

<table border="0" width="100%">
<tbody>
    <tr>

        <td align="left">&nbsp;&nbsp;&nbsp;
            <input type="submit" name="ctl00$BodyContent$wizCandidate$FinishNavigationTemplateContainerID$btnPay" value="Start Payment Process" onclick="return ShowModalDialogPage('PaymentForm.aspx');" id="ctl00_BodyContent_wizCandidate_FinishNavigationTemplateContainerID_btnPay" disabled="">    
        </td>

        <td align="right">
            <input type="submit" name="ctl00$BodyContent$wizCandidate$FinishNavigationTemplateContainerID$StepPreviousButton" value="Previous" id="ctl00_BodyContent_wizCandidate_FinishNavigationTemplateContainerID_StepPreviousButton" disabled="">
            <input type="submit" name="ctl00$BodyContent$wizCandidate$FinishNavigationTemplateContainerID$btnFinish" value="Finish" id="ctl00_BodyContent_wizCandidate_FinishNavigationTemplateContainerID_btnFinish">
        </td>

            <input type="hidden" name="ctl00$BodyContent$wizCandidate$FinishNavigationTemplateContainerID$hiddenFieldbtnFinishClicked" id="ctl00_BodyContent_wizCandidate_FinishNavigationTemplateContainerID_hiddenFieldbtnFinishClicked" value="0">

    </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_BodyContent_wizCandidate_FinishNavigationTemplateContainerID_btnFinish');
Avatar of Arikkan

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(btnFinish);
Avatar of Arikkan

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.
If you put a break-point in the page load event handler, does that get hit?
Does btnFinish.UniqueID return a value?
Avatar of Arikkan

ASKER

Oh. The Page Load event handler does get called. So strange.


Does btnFinish.UniqueID return a value? --> YES

btnFinish.UniqueID = "ms__id1"
SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Arikkan

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?
Avatar of Arikkan

ASKER

ok. I used this:

__doPostBack('ms__id1', 'btnFinish_Click__JavaScript');


I can access the passed PARAMETER in PAGE_LOAD()

 Dim passedArgument As String = Request.Params.Get("__EVENTARGUMENT")

If passedArgument = "btnFinish_Click__JavaScript" 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.WizardNavigationEventArgs) Handles wizCandidate.FinishButtonClick
     ....
     ....
     ....
     RaiseEvent CandidateFilingFinishButtonClick(sender, e)       //Now this won't fire ????
   
     ....
     ....
     ....
     ....          
End Sub
What does this do?

RaiseEvent CandidateFilingFinishButtonClick(sender, e)
Avatar of Arikkan

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 CandidateFilingFinishButtonClick As EventHandler Implements ICandidateFormationView._candidateFilingFinishButtonClick  
     ....
     ....
     ....
     ....


   Protected Sub wizCandidate_FinishButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles wizCandidate.FinishButtonClick
    ....
    ....
    ....
         RaiseEvent CandidateFilingFinishButtonClick(sender, e)
    ....
    ....          
    ....          
   End Sub


End Class
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Arikkan

ASKER

ok I will try and get back to you. Thanks.
Avatar of Arikkan

ASKER

I called the "FinishButtonClick(....)" in "PAGE_LOAD(....)
Avatar of Arikkan

ASKER

Thanks a lot Bob.  :-)