session是保存在服务器端的,客户端得不到。
客户端可以得到cookie
<script language="javascript">
function getCookie(){
alert(document.cookie);
}
</script>
客户端可以得到cookie
<script language="javascript">
function getCookie(){
alert(document.cookie);
}
</script>
.aspx
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>testgrid</title>
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<script language="javascript">
<!--
function tst()
{
alert(document.all.Hidden1.value);
}
-->
</script>
</HEAD>
<body onunload="tst()" MS_POSITIONING="GridLayout" XMLNS:ignav="http://schemas.infragistics.com/ASPNET/WebControls/WebTree" XMLNS:igtbar="http://schemas.infragistics.com/ASPNET/WebControls/UltraWebToolbar">
<form id="testgrid" method="post" runat="server">
<input
id=Hidden1 type=hidden value="<%#MySession%>" name=Hidden1
runat="server">
<asp:button id="Button1" style="Z-INDEX: 101; LEFT: 228px; POSITION: absolute; TOP: 84px" runat="server" Width="100px" Text="Button"></asp:button><asp:textbox id="TextBox1" style="Z-INDEX: 102; LEFT: 224px; POSITION: absolute; TOP: 36px" runat="server"></asp:textbox><asp:label id=Label1 style="Z-INDEX: 103; LEFT: 456px; POSITION: absolute; TOP: 56px" runat="server" Text="<%# MySession %>">
</asp:label><INPUT style="Z-INDEX: 104; LEFT: 344px; POSITION: absolute; TOP: 84px" onclick="tst()" type="button" value="Button">
</form>
</body>
</HTML>
.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace web1
{
/// <summary>
/// testgrid 的摘要说明。
/// </summary>
public class testgrid : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputHidden Hidden1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label Label1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
this.DataBind();
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
} private void Button1_Click(object sender, System.EventArgs e)
{
if( this.TextBox1.Text.Length > 0 )
Session["mys"] = this.TextBox1.Text;
this.DataBind();
}
public string MySession
{
get
{
if( Session["mys"] == null )
return "";
else
return Session["mys"].ToString();
}
}
}
}
cookie:document.cookie
Session:<%=Session["..."]%>