简体   繁体   中英

Need to call a Javascript method from the code-behind

I have my code-behind class and there I have an EventListener , I need to refresh the whole page when my EventListener catches an ´Event´.

How do I do this? I have a JavaScript function in my client-side window.location.reload(true) .

The problem is that the javascript never gets executed.

Code-Behind:

private void WebResponse_Msg(object sender, EventArgs e){
     ClientScript.RegisterClientScriptBlock(typeof(Page), Guid.NewGuid().ToString(), "refresh();", true);
 }

JS:

<script type="text/javascript">
    function refresh() {
        window.location.reload(true);
    }
</script>

Server side code can not trigger javascript (client side code) whenever you want (like when an event is triggered).

However there are several workarounds that I mention 2 of them:

1.Using SignalR

2.Logging that event when triggered in a Session, and checking the session value by ajax periodically.

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