Description:-
In this example we explain that how to Access
Session variable in javascript or jquery.here we describes how to Access
Session at client side in asp.net.
Sometime we have requirement in application to Access the Session variable value at clientside or in javascript or jquery.
For example if we have one Application in which we have one form like Comment page and login user can Add the comment but without postback or page load. So you have to must defined a one web method that are used for inserting comment and this method is called from the clientside like Jquery/Ajax call with one parameter username.
So how it is possible because that is the real problem that I was faced. So finally we fetch the session value in javascript and then call the web method with pass parameter like username that we already fetch it from the session variable at client side javascript.
So access session variable in javascript and pass it to method and insert the record without postback is done through below code.
Sometime we have requirement in application to Access the Session variable value at clientside or in javascript or jquery.
For example if we have one Application in which we have one form like Comment page and login user can Add the comment but without postback or page load. So you have to must defined a one web method that are used for inserting comment and this method is called from the clientside like Jquery/Ajax call with one parameter username.
So how it is possible because that is the real problem that I was faced. So finally we fetch the session value in javascript and then call the web method with pass parameter like username that we already fetch it from the session variable at client side javascript.
So access session variable in javascript and pass it to method and insert the record without postback is done through below code.
there are multiple ways to access Session variable at client side at javascript as follows
Restrict the size of File when Uploaded How to Restrict the size of File when uploaded by user
Dynamically Read/Write File in asp.Net How to Read Write File in Asp.Net using C#
Solution 1:-
<script type="text/javascript">
function
GetUserName() {
var
username = '<%= Session["UserName"] %>';
alert(username);
}
</script>
Solution 2:-
<%@
Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Access
Session Value at Client Side Using JavaScript In Asp.Net</title>
<script language="javascript" type="text/javascript">
function
FetchSessionvalue() {
alert("Current
Login User is : " + document.getElementById("hdn_session").value);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="hidden" id="hdn_session" value="<%=Session["loginname"]
%>" />
<br />
<br />
</div>
</form>
<script language="javascript" type="text/javascript">
FetchSessionvalue();
</script>
</body>
</html>
0 comments:
Post a Comment