We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

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

Yurich
Yurich asked
on
Medium Priority
277 Views
Last Modified: 2012-05-06
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
Comment
Watch Question

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.

Author

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
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
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.

Author

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
That section of code is automatically generated by asp.net, you don't put it anywhere.

Author

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
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', '');

Author

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>
If you click the button does it do anything?
Oh I see, use an asp:LinkButton, not a normal asp:Button.
<asp:LinkButton ID="btnFinished" runat="server" Text="Finished" OnClick="btnFinished_Click" />      

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
cool, that works :)

thanks

Author

Commented:
thanks
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.