在我的申请表中,我使用母版页。 当我关闭任何页面时其中一个智能页面我要删除所有会话和缓存。 我只能在我关闭的页面上这样做。 对于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 () {
更多推荐
服务器端调用页面关闭事件
发布评论