简体   繁体   中英

How do you raise a server-side event from javascript?

I have a control that is basically functioning as a client-side timer countdown control.

I want to fire a server-side event when the count down has reached a certain time.

Does anyone have an idea how this could be done?

So, when timer counts down to 0, a server-side event is fired.

您可能希望使用AJAX进行服务器端调用。

When you render the page create a client-side button that would do the action you want on postback. Then use ClientScriptManager.GetPostBackEventReference passing in the control as reference and add a client-side event to it using attributes as the example at the bottom of that link shows.

You can then see the Javascript it renders and use that in your function to trigger the correct server-side event.

IIRC,有一个计时器服务器控件应为您执行此操作。

Below is one way to notify the server of an event. You are really firing a client side event but then communicating with the server. This is if you aren't living in an AJAX world though.

function NotifyServer()
{
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    xmlHttp.onreadystatechange = OnNotifyServerComplete;
    xmlHttp.open("GET", "serverpage.aspx", true);
    xmlHttp.send();
}
function OnNotifyServerComplete()
{
    if (xmlHttp.readyState == 4)
    {
        if (xmlHttp.status == 200)
        {
            if (xmlHttp.responseText != "1")
                //do something
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM