Criando um formulário simples, para gravar e Ler a Session:
Código do Formulário:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="ExemploSession.aspx.cs" Inherits="ExemploSession" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<style type="text/css">
.style1
{
width: 122px;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<br />
<table style="width: 73%;">
<tr>
<td class="style1">
Nome:</td>
<td>
<asp:TextBox ID="txtNome" runat="server" Width="217px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style1">
Senha:</td>
<td>
<asp:TextBox ID="txtSenha" runat="server" TextMode="Password" Width="217px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style1">
</td>
<td>
<asp:Button ID="btnGravar" runat="server" onclick="btnGravar_Click"
Text="Gravar" />
<asp:Button ID="btnLer" runat="server" onclick="btnLer_Click"
Text="Ler " />
</td>
</tr>
<tr>
<td class="style1">
</td>
<td>
<asp:Label ID="lblMsg" runat="server"></asp:Label>
</td>
</tr>
</table>
</asp:Content>
No evento click do botão Gravar:
protected void btnGravar_Click(object sender, EventArgs e)
{
//Salvando os valores na Session
Session["nome"] = txtNome.Text.Trim();
Session["senha"] = txtSenha.Text.Trim();
lblMsg.Text = "Sessão Salva";
}
Código no Evento click do botão Ler:
protected void btnLer_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty((string)Session["nome"]))
{
lblMsg.Text = string.Format("Nome: {0}, Senha: {1} - SessionID: {2}",
Session["nome"],
Session["senha"],
Session.SessionID);
//limpa a sessao
Session.Clear();
//abandona a sessao
Session.Abandon();
}
Bons estudos moçada!