Search code examples
datatableparameter-passingpass-by-referencegeneric-handler

Pass By Reference to Generic Handler(.ashx) in Asp.net


I followed this post:DataTable using Server Side Processing.
Inside default.aspx, I am calling .ashx using:

<script type="text/javascript">
$(function () {
    $('#example').dataTable({
        'bProcessing': true,
        'bServerSide': true,
        'sAjaxSource': '/data.ashx'
    });
});

On Page_Load event of defaut.aspx:

Employee emp=new Employee();
emp.name="abc";
emp.addr="pqr";
emp.phone="123";

Where Employee is the name of Class.
How can I pass the Employee Object to Data.ashx?

I tried using HttpContext.Current.Session but is shows Session object as null.
Please Help.


Solution

  • To Access the session Inside I used IRequiresSessionState interface as below:

    public class Data : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {
         public void ProcessRequest(HttpContext context)
         {
             // get Employee object from session here
             Employee emp =(Employee)HttpContext.Current.Session["employee"];
         }
    }
    

    Ofcourse to set an Employee object in session, on Page_Load event of defaut.aspx:

    Employee emp=new Employee();
    emp.name="abc";
    emp.addr="pqr";
    emp.phone="123";
    
    HttpContext.Current.Session["employee"]=emp;
    

    NOTE:
    There are two interfaces present to make access available to HttpSession in Generic Handler:

    1. IRequiresSessionState
      Using this interface we can read and write the session variables.

    2. IReadOnlySessionState
      Using this interface we can only read and cannot write or edit the session variables.

    For more information check this link: IRequiresSessionState vs IReadOnlySessionState