你如何从javascript中提出服务器端事件?

时间:2021-08-28 15:10:09

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.

因此,当计时器倒计时为0时,将触发服务器端事件。

4 个解决方案

#1


3  

You would probably want to use AJAX to make your server side call.

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

#2


2  

IIRC, there is a timer server control that should do this for you.

IIRC,有一个计时器服务器控件,应该为你做这个。

#3


2  

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.

当您呈现页面时,创建一个客户端按钮,该按钮将在回发时执行您想要的操作。然后使用ClientScriptManager.GetPostBackEventReference作为引用传入控件,并使用属性添加客户端事件,如该链接底部的示例所示。

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

然后,您可以看到它呈现的Javascript,并在您的函数中使用它来触发正确的服务器端事件。

#4


1  

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.

以下是向服务器通知事件的一种方法。您实际上正在触发客户端事件,但随后与服务器通信。如果你不是生活在AJAX世界中的话。

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

}

#1


3  

You would probably want to use AJAX to make your server side call.

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

#2


2  

IIRC, there is a timer server control that should do this for you.

IIRC,有一个计时器服务器控件,应该为你做这个。

#3


2  

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.

当您呈现页面时,创建一个客户端按钮,该按钮将在回发时执行您想要的操作。然后使用ClientScriptManager.GetPostBackEventReference作为引用传入控件,并使用属性添加客户端事件,如该链接底部的示例所示。

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

然后,您可以看到它呈现的Javascript,并在您的函数中使用它来触发正确的服务器端事件。

#4


1  

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.

以下是向服务器通知事件的一种方法。您实际上正在触发客户端事件,但随后与服务器通信。如果你不是生活在AJAX世界中的话。

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

}