Posts Tagged ‘dotnet’

Exemplo de sessão no ASP.NET

agosto 21st, 2014

Crie os respectivos arquivos index.aspx.cs e index.aspx
Abaixo o index.aspx.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class index : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
 
    protected void btnCreateSession_Click(object sender, EventArgs e)
    {
        Session["Username"] = txtUserName.Text.Trim();
    }
 
    protected void btnRetrieveSession_Click(object sender, EventArgs e)
    {
        DisplaySessionValue();
    }
 
    protected void btnRemoveSession_Click(object sender, EventArgs e)
    {
        Session.Remove("Username");
 
        DisplaySessionValue();
    }
 
    protected void btnRemoveAll_Click(object sender, EventArgs e)
    {
        Session.RemoveAll();
 
        DisplaySessionValue();
    }
 
    private void DisplaySessionValue()
    {
        if (Session["Username"] != null)
            lblSessionValue.Text = Convert.ToString(Session["Username"]);
        else
            lblSessionValue.Text = "No Value has been stored in session";
    }
}

Abaixo o index.aspx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="index" %>
<!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>ASP.net How to use session</title>
    <style type="text/css">
     .txtInput
     {
         width:250px; height:28px; paddiang:3px;
     }   
     div
     {
         margin:5px;
     }
     .validator
     {
         color:Red;
     }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
         <fieldset>
            <h3>How to use Session</h3>
            <div>
            Userame:<br />
            <asp:TextBox ID="txtUserName" runat="server" MaxLength="50"></asp:TextBox>
            <asp:RequiredFieldValidator ID="rfvFirstName" runat="server" Display="Dynamic"
             CssClass="validator"  ControlToValidate="txtUserName"
             ErrorMessage="Username is required"></asp:RequiredFieldValidator>
            </div>
            <div>
                <asp:Button ID="btnCreateSession" runat="server" 
                    Text="Click to Create Session" onclick="btnCreateSession_Click" />
                &nbsp;
                <asp:Button ID="btnRetrieveSession" runat="server" CausesValidation="false" 
                    Text="Click to Retrieve Session Value" onclick="btnRetrieveSession_Click" />
                &nbsp;
                <asp:Button ID="btnRemoveSession" runat="server" CausesValidation="false" 
                    Text="Click to Remove Session Value" onclick="btnRemoveSession_Click" />
                &nbsp;
                <asp:Button ID="btnRemoveAll" runat="server" CausesValidation="false" 
                    Text="Click to Remove All Sessions" onclick="btnRemoveAll_Click" />
            </div>
            <p>
                Note: 1st create a session by providing user name in text field, then you can retrieve the value from session.
            </p>
            <div>
                Value stored in Session: 
                <strong><asp:Label ID="lblSessionValue" runat="server"></asp:Label></strong>
            </div>
        </fieldset>
    </div>
    </form>
</body>
</html>

Unable to make the session state request to the session state server

julho 27th, 2011

Esse erro estava ocorrendo quando tinha recém instalado o asp.net em um windows 2003.

Unable to make the session state request to the session state server.
Please ensure that the ASP.NET State service is started and that the client and server ports are the same.
If the server is on a remote machine, please ensure that it accepts remote requests by checking the value of HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\aspnet_state\Parameters\AllowRemoteConnection.
If the server is on the local machine, and if the before mentioned registry value does not exist or is set to 0, then the state server connection string must use either ‘localhost’ or ‘127.0.0.1’ as the server name.

A solução que achei foi a seguinte: ativar o “ASP.NET State Service”, nos serviços do servidor, em ferramentas administrativas, no painel de controle. Configura ele para iniciar automaticamente, e inicia o serviço, reinicia o IIS e faz um novo teste, depois disso deve funcionar.