Hello. I have a java script function which presents a countdown timer.
when the timer reaches zero, I want to somehow activate a function on the server side in the code-behind.
I understand it is impossible to directly call a server-side function, but is there a simple way to still fire it?
I want to fire the (event-handler) code behind function: protected void ButtonFinish_Click(object sender, EventArgs e).
my java script code:
var _countDowncontainer = 0;
var _currentSeconds = 0;
function ActivateCountDown(strContainerID, initialValue) {
_countDowncontainer = document.getElementById(strContainerID);
if (!_countDowncontainer) {
alert("count down error: container does not exist: " + strContainerID +
"\nmake sure html element with this ID exists");
return;
}
Saturn888
Member
13 Points
66 Posts
calling code behind method from javascript function
Dec 27, 2010 03:01 PM|LINK
Hello. I have a java script function which presents a countdown timer.
when the timer reaches zero, I want to somehow activate a function on the server side in the code-behind.
I understand it is impossible to directly call a server-side function, but is there a simple way to still fire it?
I want to fire the (event-handler) code behind function: protected void ButtonFinish_Click(object sender, EventArgs e).
my java script code:
var _countDowncontainer = 0;
var _currentSeconds = 0;
function ActivateCountDown(strContainerID, initialValue) {
_countDowncontainer = document.getElementById(strContainerID);
if (!_countDowncontainer) {
alert("count down error: container does not exist: " + strContainerID +
"\nmake sure html element with this ID exists");
return;
}
SetCountdownText(initialValue);
window.setTimeout("CountDownTick()", 1000);
}
function CountDownTick() {
if (_currentSeconds <= 0) {
alert("time is up!");
//I want to fire the server-side method from here
return;
}
SetCountdownText(_currentSeconds - 1);
window.setTimeout("CountDownTick()", 1000);
}
function SetCountdownText(seconds) {
//store:
_currentSeconds = seconds;
//get minutes:
var minutes = parseInt(seconds / 60);
//shrink:
seconds = (seconds % 60);
//get hours:
var hours = parseInt(minutes / 60);
//shrink:
minutes = (minutes % 60);
//build text:
var strText = AddZero(hours) + ":" + AddZero(minutes) + ":" + AddZero(seconds);
//apply:
_countDowncontainer.innerHTML = strText;
}
function AddZero(num) {
return ((num >= 0) && (num < 10)) ? "0" + num : num + "";
}
thanks!
AZMatt
Star
10648 Points
1896 Posts
Re: calling code behind method from javascript function
Dec 27, 2010 03:07 PM|LINK
My method is a little ugly, but it works. You add a hidden button to your page and use $get("<%= Button1.ClientID %>").click();
Matt
Ahmad Kmalde...
Member
122 Points
35 Posts
Re: calling code behind method from javascript function
Dec 27, 2010 03:15 PM|LINK
first of all you will need a asp control like button or ... but you can hide it with css any way .. use this javascript function:
<script type="text/javascript"> var prm = Sys.WebForms.PageRequestManager.getInstance(); function PostBack(ControlID) { prm._doPostBack(ControlID, ''); } </Script>
now you can call any asp control like this
<div onclick="PostBack('<%= theControlID.ClientID %>');"> </div>Saturn888
Member
13 Points
66 Posts
Re: calling code behind method from javascript function
Dec 27, 2010 03:39 PM|LINK
can you be more specific of how it works?
I tried
<asp:Button ID="Button1" runat="server" Text="Invisible Button" Visible="False"
onclick="Button1_Click" />
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "hello from js code";
}
and from js code:
$get("<%= Button1.ClientID %>").click();
Ahmad Kmalde...
Member
122 Points
35 Posts
Re: calling code behind method from javascript function
Dec 27, 2010 03:47 PM|LINK
this is your example with my solution just take it as itis and delete the last line you post for javascript :
<asp:Button ID="Button1" runat="server" Text="Invisible Button" Visible="False"
onclick="Button1_Click" />
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "hello from js code";
}
and for javascript:
PostBack('<%= Button1.ClientID %>');
SGWellens
All-Star
126031 Points
10310 Posts
Moderator
Re: calling code behind method from javascript function
Dec 27, 2010 03:51 PM|LINK
That is incorrect.
Here is an example of how to do it.
http://weblogs.asp.net/stevewellens/archive/2009/06/09/ah-ah-ah-ah-staying-alive-staying-alive.aspx
My blog
Ahmad Kmalde...
Member
122 Points
35 Posts
Re: calling code behind method from javascript function
Dec 27, 2010 03:57 PM|LINK
have you try it first case it's very fine with me and very simple to use !!!!?
Saturn888
Member
13 Points
66 Posts
Re: calling code behind method from javascript function
Dec 27, 2010 04:02 PM|LINK
thanks. I tried but i get the following error:
"Microsoft JScript runtime error: 'Sys' is undefined"
Ahmad Kmalde...
Member
122 Points
35 Posts
Re: calling code behind method from javascript function
Dec 27, 2010 04:10 PM|LINK
my friend you need to use ScriptManager in the same page
<asp:ScriptManager ID="ScriptManager1" runat="server" LoadScriptsBeforeUI="true"> </asp:ScriptManager>Saturn888
Member
13 Points
66 Posts
Re: calling code behind method from javascript function
Dec 27, 2010 04:23 PM|LINK
can you show where do you put it?