Client Side Session Variables.

Using jQuery, Ajax, and WebServices to provide interop between ASP Session and Client Side

So been a bit isnce i posted, have been working on a lot of legacy system stuff, so nothing interesting. However I am finally back to learning new technologies and thought i would make that the subject of this blog.

 

 

Code: asp.net
  1. Imports System.Web.Services
  2. Imports System.Web.Services.Protocols
  3. Imports System.ComponentModel
  4. Imports Oracle.DataAccess.Client
  5. ' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
  6. <System.Web.Script.Services.ScriptService()> _
  7. <System.Web.Services.WebService(Namespace:="http://canfor.ca/")>_
  8. <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
  9. <ToolboxItem(False)> _
  10. Public Class session    
  11. Inherits System.Web.Services.WebService    
  12. ''' <summary>    
  13. ''' This sets the session variable for the application and user listed below.    
  14. ''' </summary>    
  15. ''' <param name="varValue">This is the value for the session variable</param>    
  16. ''' <param name="UserID">The Users UserID</param>    
  17. ''' <param name="appid">The application ID as listed in the APPLICATION Table</param>    
  18. ''' <param name="varName">What is the session variable name</param>    
  19. ''' <remarks></remarks>    
  20. <WebMethod()> _
  21.     Public Sub SetSessionVar(ByVal varValue As String, ByVal UserID As String, ByVal appid As Integer, ByVal varName As String)        
  22. Dim conn As New OracleConnection(ConfigurationManager.ConnectionStrings("WEB_APPS_DEV").ConnectionString)       Dim comm As New OracleCommand        
  23. Dim strSQL As String        
  24. strSQL = "PKG_SESSION_UTIL.SET_SESSION_VAR"
  25.         comm.CommandText = strSQL
  26.         comm.Connection = conn
  27.         comm.CommandType = CommandType.StoredProcedure
  28.         comm.Parameters.Add(New OracleParameter("p_userid", UserID))
  29.         comm.Parameters.Add(New OracleParameter("p_applid", appid))
  30.         comm.Parameters.Add(New OracleParameter("p_sessionvar", varName))
  31.         comm.Parameters.Add(New OracleParameter("p_varvalue", varValue))
  32.         conn.Open()
  33.         comm.ExecuteNonQuery()
  34.         conn.Close()
  35.     End Sub
  36. End Class

 

Code: sql
  1. CREATE OR REPLACE PACKAGE PKG_SESSION_UTIL AS
  2.    PROCEDURE SET_SESSION_VAR (p_userid       IN VARCHAR2
  3.                              ,p_applid       IN NUMBER
  4.                              ,p_sessionvar      VARCHAR2
  5.                              ,p_varvalue        VARCHAR2);
  6. END PKG_SESSION_UTIL;
  7. CREATE OR REPLACE PACKAGE BODY PKG_SESSION_UTIL
  8. AS
  9.    PROCEDURE SET_SESSION_VAR (p_userid       IN VARCHAR2
  10.                              ,p_applid       IN NUMBER
  11.                              ,p_sessionvar      VARCHAR2
  12.                              ,p_varvalue        VARCHAR2)   AS
  13.       TYPE aarr_session IS TABLE OF WEB_SESSION%ROWTYPE;
  14.       v_session   aarr_session;
  15.    BEGIN
  16.       SELECT *
  17.         BULK COLLECT INTO v_session
  18.         FROM WEB_SESSION
  19.        WHERE USERID = p_userid AND applid = p_applid AND sessionvar = p_sessionvar;
  20.       IF v_session.COUNT > 0
  21.       THEN
  22.          UPDATE WEB_SESSION
  23.             SET varvalue = p_varvalue
  24.           WHERE USERID = p_userid AND applid = p_applid AND sessionvar = p_sessionvar;
  25.       ELSE
  26.          INSERT INTO WEB_SESSION (userid
  27.                                  ,applid
  28.                                  ,sessionvar
  29.                                  ,varvalue)
  30.               VALUES (p_userid
  31.                      ,p_applid
  32.                      ,p_sessionvar
  33.                      ,p_varvalue);
  34.       END IF;
  35.    END;
  36. END;

 

Code: html
  1. <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WoodlandsList.aspx.vb" Inherits="WIM.WoodlandsWebApps.WoodlandsList" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5.     <title></title>
  6.     <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.js" type="text/javascript"></script>
  7.     <%If False Then%>
  8.         <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5-vsdoc.js" type="text/javascript"></script>
  9.     <%End If%>
  10.     <script type="text/javascript">
  11.         function doStuff() {
  12.             $('#OpName').slideUp(1000, function () {
  13.                 $('#drpOperations').fadeIn(1000);
  14.             });
  15.         }
  16.         $(document).ready(function () {
  17.             $('#OpName').text($('#drpOperations option:selected').text());
  18.             $('#drpOperations').hide();
  19.             $('#drpOperations').change(function () {
  20.                 var newVal = $('#drpOperations option:selected').text();
  21.                 var ajaxData = "{ 'varValue' : '" + newVal + "' , ";
  22.                 ajaxData += "'UserID' : '" + $('#hdnUser').val() + "' ,";
  23.                 ajaxData += "'appid' : '" + $('#hdnAppId').val() + "' ,";
  24.                 ajaxData += "'varName' : 'WebOperId' }";
  25.                 $.ajax({ 
  26.                    type: "POST",
  27.                     url: "/Services/session.asmx/SetSessionVar",
  28.                     data: ajaxData ,
  29.                     contentType: "application/json; charset=utf-8",
  30.                     dataType: "json"
  31.                 });
  32.                 $('#drpOperations').slideUp(1000, function () {
  33.                     $('#OpName').text(newVal);
  34.                     $('#OpName').fadeIn(1000);
  35.                 });
  36.             });
  37.         });
  38.     </script>
  39.     <style type="text/css">
  40.         #OpName
  41.         {
  42.             font-weight:bold;
  43.             color:Maroon;
  44.         }
  45.         .OpBreadCrumb
  46.         {
  47.             float:right;
  48.             font-family:Arial;
  49.             font-size:11pt;
  50.             border:1px solid red;
  51.             vertical-align:middle;
  52.             padding-top:10px;
  53.             margin:0px;
  54.         }
  55.     </style>
  56.     </head>
  57. <body>
  58.     <form id="form1" runat="server">
  59.     <div class="OpBreadCrumb">
  60.     <asp:HiddenField ID="hdnUser" runat="server" />
  61.     <asp:HiddenField ID="hdnAppId" runat="server" />
  62.         Current Operation <span id="OpName" onclick="javascript:doStuff()">Prince George</span>
  63.     <asp:DropDownList runat="server" ID="drpOperations">    </asp:DropDownList>
  64.     </div>
  65.     <asp:Button ID="poster" runat="server" Text="PostBack" />
  66.     <asp:Label ID="lblMessage" runat="server"></asp:Label>
  67.     </form>
  68. </body>
  69. </html>

 

Code: asp.net
  1. Imports Oracle.DataAccess.ClientPublic
  2. Class WoodlandsList
  3.     Inherits System.Web.UI.Page
  4.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  5.         If Not IsPostBack Then
  6.             Dim dsOps As DataSet = CType(Cache.Get("HIPRDS_OPS"), DataSet)
  7.             Dim strSql As String
  8.             If dsOps Is Nothing Then
  9.                 strSql = " SELECT OPER_NAME, opap.APP_OPER_CODE "
  10.                 strSql &= "  FROM OPERATION op INNER JOIN OPERATION_APPL opap ON OP.OPER_ID = OPAP.OPER_ID "
  11.                 strSql &= " WHERE OPAP.APP_ID = 141 "
  12.                 Dim conn As New OracleConnection( _ ConfigurationManager.ConnectionStrings("WEB_APPS_DEV").ConnectionString)
  13.                 Dim adap As New OracleDataAdapter(strSql, conn)
  14.                 dsOps = New DataSet()                adap.Fill(dsOps)
  15.                 drpOperations.DataSource = dsOps
  16.                 drpOperations.DataTextField = "OPER_NAME"
  17.                 drpOperations.DataValueField = "APP_OPER_CODE"
  18.                 drpOperations.DataBind()
  19.                 Cache.Insert("HIPRDS_OPS", dsOps)
  20.             Else
  21.                 drpOperations.DataSource = dsOps
  22.                 drpOperations.DataTextField = "OPER_NAME"
  23.                 drpOperations.DataValueField = "APP_OPER_CODE"
  24.                 drpOperations.DataBind()
  25.             End If
  26.         End If
  27.         hdnUser.Value = Context.User.Identity.Name.Split("\")(1)        hdnAppId.Value = 141    End Sub
  28. End Class

12,802 views 0 replies