服务器端调用页面关闭事件

编程入门 行业动态 更新时间:2024-10-28 18:24:51
本文介绍了服务器端调用页面关闭事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的申请表中,我使用母版页。 当我关闭任何页面时其中一个智能页面我要删除所有会话和缓存。 我只能在我关闭的页面上这样做。 对于Ex。我有一个site.master页面。我有一个名为test.aspx的子页面 我在text.aspx.cs页面上写了下面的方法,我在'onunload'事件上调用了这个方法

Hi, In my application I am using master page. When I close any one of the chile pages in I want to delete all session and cache. I can do that only on the page I am closing. For Ex. I have a site.master page. And I have child page called test.aspx I have written the below method on text.aspx.cs page, and I am calling this method on 'onunload' event

[WebMethod] public static void BrowserCloseMethod() { //Removing all cache on closing of browser foreach (DictionaryEntry dEntry in HttpContext.Current.Cache) HttpContext.Current.Cache.Remove(dEntry.Key.ToString()); HttpContext.Current.Session.Abandon(); HttpRuntime.Close(); }

当我关闭test.aspx页面时,此代码工作正常。 但是我不能继续在所有子页面中编写这些方法。 我添加了这个方法,并尝试在site.master.cs页面上调用此方法。 但是方法没有被触发。 请帮我实现这个目标。在此先感谢

This code is working fine when I close test.aspx page. But I can not keep on writing these methods in all child pages. I added this method and tried to call this method on site.master.cs page. But the method is not triggered. Please help me achieve this. Thanks in advance

推荐答案

它不应该按预期工作,因为onunload事件将在每次回发时触发。 但是,如果您的代码按预期工作,那么应用所有子页面的最佳方式... 1.创建一个将继承自System.Web.UI.Page的BasePage 。 2.在BasePage中编写常用方法。 3.通过继承BasePage创建其他子页面。 It should not work as expected because "onunload" event will fire on every postback. However, if your code is working as expected, then the best way to apply all child pages... 1. Create a BasePage which will be inherited from System.Web.UI.Page. 2. Write your common methods in BasePage. 3. Create other child pages by inheriting BasePage.

你不能将WebMethod添加到母版页(没有内容页面时母版页不能存在),所以你唯一的选择就是创建一个web服务(asmx)并为其添加一个静态WebMethod修饰方法。 此方法可以从JQuery调用为 MyWebService.asmx / MyWebMethod 。这样做的javascript可以很常见并链接到主人... WebServer(Static.asmx) You can not add WebMethod to master page (master page can not be exists without a content page), so the only option you got is create a web-service (asmx) and add a static WebMethod decorated method to it. This method can be called from JQuery as MyWebService.asmx/MyWebMethod. The javascript to do so can be common and linked to the master... WebServer (Static.asmx) [ScriptService] public class Static : WebService { [WebMethod] [ScriptMethod( UseHttpGet = false, ResponseFormat = ResponseFormat.Json )] public string HelloWorld ( ) { JavaScriptSerializer oJavaScriptSerializer = new JavaScriptSerializer( ); return ( oJavaScriptSerializer.Serialize( "Hello World" ) ); } }

母版页(Site.Master)

Master Page (Site.Master)

<html> <head runat="server"> <title></title> <script src="Scripts/jquery-2.1.1.js"></script> <asp:ContentPlaceHolder ID="head" runat="server"> </asp:ContentPlaceHolder> </head> <body> <form id="form1" runat="server"> <div> <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder> </div> </form> <script type="text/javascript">

( document )。ready ( function (){ (document).ready(function () {

更多推荐

服务器端调用页面关闭事件

本文发布于:2023-11-14 16:51:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1588006.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:服务器端   事件   页面

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!