[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2009-02-18
14
Medium Priority
?
266 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
0
Comment
Question by:Yurich
  • 8
  • 6
14 Comments
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23676800
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
 
LVL 21

Author Comment

by:Yurich
ID: 23676899
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
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23676920
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23676929
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
 
LVL 21

Author Comment

by:Yurich
ID: 23677042
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
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23677076
That section of code is automatically generated by asp.net, you don't put it anywhere.
0
 
LVL 21

Author Comment

by:Yurich
ID: 23677110
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
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23677135
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
 
LVL 21

Author Comment

by:Yurich
ID: 23677165
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
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23677173
If you click the button does it do anything?
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23677183
Oh I see, use an asp:LinkButton, not a normal asp:Button.
0
 
LVL 15

Accepted Solution

by:
aibusinesssolutions earned 2000 total points
ID: 23677190
<asp:LinkButton ID="btnFinished" runat="server" Text="Finished" OnClick="btnFinished_Click" />      
0
 
LVL 21

Author Comment

by:Yurich
ID: 23677261
cool, that works :)

thanks
0
 
LVL 21

Author Closing Comment

by:Yurich
ID: 31548568
thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question