Calling C# method from the code-behind from the javascript code

I have a javascript count-down script that runs on the ASP.NET page. After it hits 0, I'll need to do some operations and for that I need to run some methods from the code behind. How can I do that?

Thanks
LVL 21
YurichAsked:
Who is Participating?
 
aibusinesssolutionsCommented:
<asp:LinkButton ID="btnFinished" runat="server" Text="Finished" OnClick="btnFinished_Click" />      
0
 
aibusinesssolutionsCommented:
After it hits 0 you can do a postback on a button.  Just get the __doPostBack() code from a button at runtime by viewing the source, and then call that function in your javascript.

If you don't want the button to be visible, set the property to hidden after you get the code.
0
 
YurichAuthor Commented:
an example please? I'm not quite following...

I can see that in my if - else block of javascript I can do this:

document.getElementById( btnFinished ).click();

but that doesn't work :(

document provide access to the elements of the document, but not to the code behind. I was thinking just about refreshing the page, but that would be probably my last resort for now.

Thanks
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
aibusinesssolutionsCommented:
When you click a button on a form, it fires a javascript function called __doPostBack().  You can call that directly from your javascript code.

function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}


An example would be:
<script type="text/javascript">
 function doIt() {
       // do some stuff
      __doPostBack('ctl00$ContentPlaceHolder1$Button1','')
}
</script>

That would postback, and fire the function Button1_Click
0
 
aibusinesssolutionsCommented:
By the way, that example is assuming your page is in a master page.  If not you would modify the Button1 string to match the name in the source code.
0
 
YurichAuthor Commented:
where do I put this code excatly?

function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}

if it's supposed to be in javascript section of my ASP.NET page, what is theForm then? is it document.form1?

I have tried to substitute theForm with document.form1, but it failed saying that __EVENTTARGET and __EVENTARGUMENT were nulls or not objects.

thanks
0
 
aibusinesssolutionsCommented:
That section of code is automatically generated by asp.net, you don't put it anywhere.
0
 
YurichAuthor Commented:
oh, ok.

__doPostBack('ctl00$ContentPlaceHolder1$Button1','')

if I just put the ID of my button into this, it will fail asking for an object, e.g.

...
else
   __doPostBack( 'btnFinished', '' )


I can see that you have in some kinda fancy format ('ctl00$...') - where does it come from? I looked at the generated html page, it's still btnFinished, so what should I use for this?

thanks
0
 
aibusinesssolutionsCommented:
the ctl00$ContentPlaceHolder1$ comes from having a button on a masterpage.

If you view the source of the page, and the ID is just btnFinished, then just do that

__doPostBack('btnFinished', '');
0
 
YurichAuthor Commented:
that's exactly how I do it and it fails saying object expected. that's the full javascript code I'm using:

            function CountDown( time, output )
            {
                  // get hours, minutes, and seconds parts
                  hours = Math.floor( time / 3600 )
                  minutes = Math.floor(( time - hours * 3600 ) / 60 )
                  seconds = time - hours * 3600 - minutes * 60
                  
                  // pad zeros if necessary
                  seconds = AddZeros( seconds )
                  minutes = AddZeros( minutes )
                  
                  // show the result
                  document.getElementById( output ).innerHTML = hours + ":" + minutes + ":" + seconds
                  
                  // decrement the counter
                  if( time > 0 )
                        setTimeout( 'CountDown( ' + --time + ', "' + output + '")', 1000)
                  else
                        __doPostBack('btnFinished', '');
            }

:(

that's the bit with the button:
            <asp:Button ID="btnFinished" runat="server" OnClick="btnFinished_Click" Text="Button" /></div>
0
 
aibusinesssolutionsCommented:
If you click the button does it do anything?
0
 
aibusinesssolutionsCommented:
Oh I see, use an asp:LinkButton, not a normal asp:Button.
0
 
YurichAuthor Commented:
cool, that works :)

thanks
0
 
YurichAuthor Commented:
thanks
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.