Call server side code from client side

  • Thread starter Thread starter Borr
  • Start date Start date
B

Borr

Hi,

I have an ASP .NET page, that runs client side timer that
does something on the Server side and after that loads
another page. So I have on client side something like :

function OnPageLoad()
{
setTimeout("OnTimer()", 10000);
}

function OnTimer()
{
ServerMethod();
}

<body onload="OnPageLoad()">
<form id="Form1" method="post" runat="server">

It does not work, and I understand why - client side does
not know what is ServerMethod();

How to call server side method from the timer callback ?
Meanwhile I found workaround : created in Form1 tiny
disabled button DummyBtn "runat=server", defined such
server-side event handler for click :

void DummyBtn_ServerClick(object obj,EventArgs e)
{
ServerMethod();
}

and on the client side in OnTimer method wrote

Form1.DummyBtn.click();

So the timer simulates click of DummyBtn, click runs its
event handler, the event handler in turn calls the server
method.

It works, but it is ugly solution. I 'd like to call
ServerMethod() from the timer callback directly, without
tricks like this. Is it possible ?
 
there is no direct live connection between the client side and server side,
because HTTP is a stateless connetion type.

Unlesss you use something like ActiveX, or Java applet, the only way you can
communicate between the client and server sides are doing the form posting.

Alone this line, you can call form.submit() method in your timer method and
activate the server side methods.

Jianjun
 
That's not entirely true, you can use server side blocks to execute methods
on the server-side although it is a somewhat advanced technique.
 
Back
Top