Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have the code in my WebForm1.aspx as follows:
XML
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
    CodeBehind="WebForm1.aspx.cs" Inherits="Kaizen.UI.WEB.Kaizen2G.WebForm1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <script>
        setInterval(function () { CheckSession(); }, 10000);

        function CheckSession() {
            debugger;
            $.ajax({
                url: 'WebForm1.aspx/CheckSession',
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                dataType: "json",
                success: function (result) {

                    if (result.d != '') {
                        alert(result.d);
                        manageOverlay(false);
                        isAdded = false;
                    }

                },
                error: function (error) {
                    manageOverlay(false);
                    alert(error.responseText);
                    isAdded = false;

                },
                async: false
            })
        };
    </script>



I have code in my .cs page as follows
C#
namespace Kaizen.UI.WEB.Kaizen2G
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Kaizen.Framework;

    /// <summary>
    /// The page looad
    /// </summary>
    public partial class WebForm1 : System.Web.UI.Page
    {
        /// <summary>
        /// Check User Session
        /// </summary>
        [WebMethod]
        public void CheckSession()
        {
            if (Session[KaizenConstants.CURRENTLOGGEDINUSER] == null)
            {
                Response.Redirect("Login.aspx");
            }
        }

        /// <summary>
        /// The page event
        /// </summary>
        /// <param name="sender">The Sender</param>
        /// <param name="e">The Eventsargs</param>
        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }
}

on page load of aspx page i wrote this :
JavaScript
$(function () {
            try {
                debugger;
                $.ajaxSetup({
                    async: false
                });
             
                setInterval(function () { CheckSession(); }, 10000);
            }
            catch (exception) {
                manageOverlay(false);
                alert(exception);
            }
        });


Now when i am calling method from ajax i am getting the error as follows.
500(internal Server Error)

why is it so .
please guide me .
i want to call the method from WebForm1.aspx.cs

Thanks
Harshal
Posted
Updated 8-May-14 5:22am
v4
Comments
R Harshal 8-May-14 10:16am    
Anyone can help me please ..
FluffySaysNo 8-May-14 10:45am    
your webmethod must be static, otherwise it is not accessible outside of the server context of the page. This context drops after the initial page life cycle events for loading the page. This link will fully explain the "Why?" : http://encosia.com/why-do-aspnet-ajax-page-methods-have-to-be-static/
FluffySaysNo 8-May-14 12:46pm    
Don't forget to mark an answer if it answers, or leads you to an answer for, your question. ;D

1 solution

Change :
C#
/// <summary>
/// Check User Session
/// </summary>
[WebMethod]
public void CheckSession()
{
    if (Session[KaizenConstants.CURRENTLOGGEDINUSER] == null)
    {
        Response.Redirect("Login.aspx");
    }
}

to
C#
/// <summary>
/// Check User Session
/// </summary>
[WebMethod]
public static AjaxResponse<bool> CheckSession()
{
    AjaxResponse<bool> oResponse = new AjaxResponse<bool>();

    oResponse.Data = Session[KaizenConstants.CURRENTLOGGEDINUSER] == null;

    return oResponse
}


Link : Why do asp.net ajax page methods have to be static?

This is a general container class

C#
public class AjaxResponse<T>
{
   /// <summary>
   /// Constructor
   /// </summary>

   public AjaxResponse()
   {
      Status   = ResponseStatus.OK.ToString();
      ErrorMsg = "";
   }

   public string Status    { get; set; }
   public string ErrorMsg  { get; set; }
   public T      Data      { get; set; }
}


success: function (result) {

    if (result.d != '')
        result = result.d
    if (result.Data == 'True') {
        /* Session has timed out, or user was not logged in */
    }

}
 
Share this answer
 
v3
Comments
R Harshal 8-May-14 11:56am    
yeah but its give me error as follows:

Error 45 An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Session.get' G:\Quantum IndiaProjects\Code\Kaizen.UI\Kaizen.UI.WEB\Kaizen2G\WebForm1.aspx.cs 32 17 Kaizen.UI.WEB

I have tried this but its give me error.,Kindly help
Thanks
Harshal
FluffySaysNo 8-May-14 12:00pm    
hmm. You will need to use a different route to access the session... Maybe HttpContext.Current.Session[KaizenConstants.CURRENTLOGGEDINUSER] will work for you
FluffySaysNo 8-May-14 12:02pm    
Also, changing [WebMethod] to [WebMethod(EnableSession = true)] should help if the session is not available this way
R Harshal 8-May-14 12:16pm    
Please help Fluffy ..
R Harshal 8-May-14 12:16pm    
for session its work and what about for Response.Redirect .I am getting the same error .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900