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?
ArikkanAsked:
Who is Participating?
 
Bob LearnedCommented:
If you are just calling a method, then I don't see a reason to raise an event.  I would just call the method there:

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

Open in new window

0
 
Bob LearnedCommented:
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
0
 
ArikkanAuthor Commented:
I tried it and got this message:

"JavaScript runtime error: Object doesn't support property or method 'replace'"
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Bob LearnedCommented:
Where did you get that error?
0
 
ArikkanAuthor Commented:
I got it on __doPostBack(...) statement as below:

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

2) Are you able to debug JavaScript?
0
 
ArikkanAuthor Commented:
1. Here I am using IE (10.0.9200.17267)
2. I am able to debug JavaScript
0
 
Bob LearnedCommented:
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.
0
 
ArikkanAuthor Commented:
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, '_');
    }
0
 
Bob LearnedCommented:
How is "btnFinish" defined in the HTML?
0
 
ArikkanAuthor Commented:
<%@ 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>
0
 
Bob LearnedCommented:
Sorry, I meant to say the rendered output (View Source in browser).  I wanted to see how the unique ID/client ID was generated.
0
 
ArikkanAuthor Commented:
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>
0
 
Bob LearnedCommented:
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');
0
 
ArikkanAuthor Commented:
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);
0
 
ArikkanAuthor Commented:
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.
0
 
Bob LearnedCommented:
If you put a break-point in the page load event handler, does that get hit?
0
 
Bob LearnedCommented:
Does btnFinish.UniqueID return a value?
0
 
ArikkanAuthor Commented:
Oh. The Page Load event handler does get called. So strange.


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

btnFinish.UniqueID = "ms__id1"
0
 
Bob LearnedCommented:
Try using UniqueID:

__doPostBack('ms__id1');
0
 
ArikkanAuthor Commented:
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?
0
 
ArikkanAuthor Commented:
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
0
 
Bob LearnedCommented:
What does this do?

RaiseEvent CandidateFilingFinishButtonClick(sender, e)
0
 
ArikkanAuthor Commented:
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
0
 
ArikkanAuthor Commented:
ok I will try and get back to you. Thanks.
0
 
ArikkanAuthor Commented:
I called the "FinishButtonClick(....)" in "PAGE_LOAD(....)
0
 
ArikkanAuthor Commented:
Thanks a lot Bob.  :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.