I tried this because I wanted to access session variable set from client side in to my code behind, but it didnt work. I dont know what am I doing wrong? but I was able to set session vriable in code behind and access it on client side, but the session variable
set on client side, shoe me as - ' + myvar + ' I dont know why?
can you please reply me back with the solution?
Thanks in advance.
Mitesh Patel.
Microsoft Corporation.
Redmond, Washington.
United States.
mtp2408
Member
4 Points
3 Posts
Re: How can I set session variables with JavaScript
Apr 23, 2011 12:17 AM|LINK
Hi!
i tried this.........
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!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 runat="server">
<title></title>
<script type="text/javascript">
function OpenReconcile(referenceNumber) {
alert('<%=Session["temp"] %>');
var myvar = "Divyesh";
'<%Session["temp"] = "' + myvar + '"; %>';
alert('<%=Session["temp"] %>');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<div/>
<asp:Button ID="Button1" runat="server" Text="Button JavaScricpt" OnClientClick="OpenReconcile('referenceNumber'); return false;" />
<asp:Button ID="Button2" runat="server" Text="Button Only Text" OnClick="button2click" />
<asp:Button ID="Button3" runat="server" Text="Button Set Session" OnClick="button3Click" />
</div>
</form>
</body>
</html>
Code behind..........
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void button2click(object sender, EventArgs e)
{
TextBox1.Text = Session["temp"].ToString();
}
protected void button3Click(object sender, EventArgs e)
{
TextBox2.Text = Session["temp"].ToString();
Session["temp"] = "Mitesh";
TextBox1.Text = Session["temp"].ToString();
}
}
}
I tried this because I wanted to access session variable set from client side in to my code behind, but it didnt work. I dont know what am I doing wrong? but I was able to set session vriable in code behind and access it on client side, but the session variable set on client side, shoe me as - ' + myvar + ' I dont know why?
can you please reply me back with the solution?
Thanks in advance.
Microsoft Corporation.
Redmond, Washington.
United States.