Enabling sessionState

The sessionState allow you to store information not in the ViewState, but in the HttpSession object. See MSDN.

To enable the sessionState, you will have to modify your web.config. All lines which need to be modified, are within the <system.web> tag:

In the default settings, the httpModule for the session state is commented out. We will simply remove the “<!–” and “–>” around the line.

   1: <httpModules>
<pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;border-top-style:none;line-height:12pt;padding-top:0px;font-family:consolas, 'Courier New', courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;border-bottom-style:none"><span style="color:#606060">   2:</span>   <span style="color:#0000ff">&lt;</span><span style="color:#800000">clear</span> <span style="color:#0000ff">/&gt;</span></pre>

<pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;border-top-style:none;line-height:12pt;padding-top:0px;font-family:consolas, 'Courier New', courier, monospace;border-right-style:none;border-left-style:none;background-color:white;border-bottom-style:none"><span style="color:#606060">   3:</span>     ...</pre>

<pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;border-top-style:none;line-height:12pt;padding-top:0px;font-family:consolas, 'Courier New', courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;border-bottom-style:none"><span style="color:#606060">   4:</span>   <span style="color:#0000ff">&lt;</span><span style="color:#800000">add</span> <span style="color:#ff0000">name</span><span style="color:#0000ff">="Session"</span> <span style="color:#ff0000">type</span><span style="color:#0000ff">="System.Web.SessionState.SessionStateModule"</span><span style="color:#0000ff">/&gt;</span></pre>

<pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;border-top-style:none;line-height:12pt;padding-top:0px;font-family:consolas, 'Courier New', courier, monospace;border-right-style:none;border-left-style:none;background-color:white;border-bottom-style:none"><span style="color:#606060">   5:</span>     ...</pre>

<pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;border-top-style:none;line-height:12pt;padding-top:0px;font-family:consolas, 'Courier New', courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;border-bottom-style:none"><span style="color:#606060">   6:</span> <span style="color:#0000ff">&lt;/</span><span style="color:#800000">httpModules</span><span style="color:#0000ff">&gt;</span></pre>

 

Change the value for enableSessionState from “false” to “true”:

   1: <pages enableSessionState="true" enableViewState="true" ...>

That’s it. Now you can use the session state.

Tags: