session variable, viewstare variable and hidden field return null valueshttp://forums.asp.net/t/1800757.aspx/1?session+variable+viewstare+variable+and+hidden+field+return+null+valuesFri, 18 May 2012 04:06:23 -040018007574968119http://forums.asp.net/p/1800757/4968119.aspx/1?session+variable+viewstare+variable+and+hidden+field+return+null+valuessession variable, viewstare variable and hidden field return null values <p>HiI'm getting no response to session variables, viewstate variables or values held in a hidden textbox when the form performs a postback. As an example I am creating a session variable on a button click just before sending a mail within the same button click event. Session(&quot;sentm&quot;)=&quot;y&quot; - then in the ispostback event I am trying to use the session value but the value returns empty (null). I know that thesession value was set because as a test after I set it, I set a label to the session value and the Label.Text gets updated correctly - but as I stated above on the ispostback event when I call the Dim str As String = Session(&quot;sentm&quot;).ToString() I get a null value. The same happens with viewstate and also if I try to get the value of a hidden textbox which I also tried setting within the button click event, all three values are returned as null. I'm fairly new to Asp.Net and I suspect it will be some thing stupid, something I have to 'switch' on/off but I cant see what. I have a demo on another page within the same site and that carries a session over to a new page and the new page is able to use it ok.So I know it isn't anything with the web.config file. Any ideas? The imports I am usingImports System.Data Imports System.Configuration Imports System.Web Imports System.Web.Security Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Web.UI.WebControls.WebParts Imports System.Web.UI.HtmlControls Imports System.Collections.Generic Imports System.Data.SqlClient Imports System.Data.Common Imports System Imports System.Globalization Imports System.Reflection Imports System.Drawing Imports System.Web.Services Imports System.Text Imports System.Net.Mail Imports System.IO Imports System.CollectionsThanks in advance for any pearls of wisdom.</p> 2012-05-07T03:58:40-04:004978769http://forums.asp.net/p/1800757/4978769.aspx/1?Re+session+variable+viewstare+variable+and+hidden+field+return+null+valuesRe: session variable, viewstare variable and hidden field return null values <p>Hi,</p> <p>I note you have post a similar thread about this topic:</p> <p><a href="http://forums.asp.net/p/1800756/4978761.aspx/1?p=True&amp;t=634725487112450049">http://forums.asp.net/p/1800756/4978761.aspx/1?p=True&amp;t=634725487112450049</a></p> <p>You said you are a newbie for asp.net. So I decide to create a simple sample code to demonstrate how to set and get ASP.NET Session, ViewState and Hidden Field:</p> <p>Note: you firstly need to set the Session/ViewState/Hidden Field. And then retrieve their value and generally, when we get the Session/ViewState/Hidden Field, we always call if statement to determine whether they are null, this can help me to&nbsp;avoid the exception:</p> <p>Asps file:</p> <pre class="prettyprint">&lt;%@ Page Language=&quot;VB&quot; AutoEventWireup=&quot;false&quot; CodeFile=&quot;Default3.aspx.vb&quot; Inherits=&quot;Default3&quot; %&gt; &lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt; &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt; &lt;head runat=&quot;server&quot;&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt; &lt;div&gt; &lt;asp:TextBox ID=&quot;TextBox1&quot; runat=&quot;server&quot;&gt;&lt;/asp:TextBox&gt; &lt;br /&gt; &lt;asp:Label ID=&quot;Label1&quot; runat=&quot;server&quot; Text=&quot;Label&quot;&gt;&lt;/asp:Label&gt; &lt;br /&gt; &lt;br /&gt; &lt;asp:Button ID=&quot;Button1&quot; runat=&quot;server&quot; Text=&quot;Set Session&quot; /&gt; &lt;asp:Button ID=&quot;Button2&quot; runat=&quot;server&quot; Text=&quot;GetAndDispalySession&quot; /&gt; &lt;/div&gt; &lt;br /&gt; &lt;asp:Button ID=&quot;Button3&quot; runat=&quot;server&quot; Text=&quot;SetViewState&quot; /&gt; &amp;nbsp;&lt;asp:Button ID=&quot;Button4&quot; runat=&quot;server&quot; Text=&quot;GetAndDisplayViewState&quot; /&gt; &lt;br /&gt; &lt;br /&gt; &lt;asp:HiddenField ID=&quot;HiddenField1&quot; runat=&quot;server&quot; /&gt; &lt;asp:Button ID=&quot;Button5&quot; runat=&quot;server&quot; Text=&quot;SetHiddenField&quot; /&gt; &lt;asp:Button ID=&quot;Button6&quot; runat=&quot;server&quot; Text=&quot;GetAndDispalyHiddenField&quot; /&gt; &lt;br /&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p></p> <p>Code behind:</p> <pre class="prettyprint">Partial Class Default3 Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Session("name") = TextBox1.Text End Sub Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click If Session("name") IsNot Nothing Then Label1.Text = Session("name") End If End Sub Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click ViewState("name") = TextBox1.Text End Sub Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click If ViewState("name") IsNot Nothing Then Label1.Text = ViewState("name") End If End Sub Protected Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button5.Click HiddenField1.Value = TextBox1.Text End Sub Protected Sub Button6_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button6.Click If HiddenField1 IsNot Nothing Then Label1.Text = HiddenField1.Value End If End Sub End Class </pre> <p></p> <p>&nbsp;</p> <p>Just copy sample sample code to test, and you can find more information from the similar thread I provided previously.</p> <p>And question please feel free to post them.</p> 2012-05-14T03:50:17-04:004979356http://forums.asp.net/p/1800757/4979356.aspx/1?Re+session+variable+viewstare+variable+and+hidden+field+return+null+valuesRe: session variable, viewstare variable and hidden field return null values <p>Hi Mamba</p> <p>Thank you very much for the reply, and for the time taken to compile it for me. I will of course retain it as an example for when I tear my hair our again with this issue. Your example functions perfectly and the session state example is how I was using it on the page concerned - and although I have checked my session delcaration against your example so that I 'know' that what I am doing is correct the page in question still returns a null session. Like I said before on other pages the session&nbsp;carries through&nbsp;as expected, hence being stumped. The idea was to confirm that a mail had been sent and that was the reason for the 'postback because within the page I have a script call&nbsp;to run a jQuery script after the mail had gone, but I didn't want that script to run if the had page posted back for another reason. The reason for that need is because the contact form as such is actually a pop up form situated on&nbsp;the page along with other elements and several of the other elements might cause a post&nbsp;back&nbsp;- they shouldn't but this was a belt and braces approach to make certain&nbsp;that the page behaved correctly at all times - &nbsp;so I needed to isolate the&nbsp; 'call' to make sure that&nbsp;the page&nbsp;acted appropriately. But, that said, at least I know from your examples whether my declarations are correct - or not :) - and that is a brilliant start so now all I have to do is remove elements one by one and see if I can isolate the why to the problem.&nbsp;Thanks again. regards</p> 2012-05-14T09:05:19-04:004979624http://forums.asp.net/p/1800757/4979624.aspx/1?Re+session+variable+viewstare+variable+and+hidden+field+return+null+valuesRe: session variable, viewstare variable and hidden field return null values <p>Hi Mamba</p> <p>I've isolated the cause of my problem which is; &nbsp;I am setting the session variable to 'y'&nbsp;on the button click which then sends the mail.</p> <p>I was then attempting via the ispostback on the page load to confirm&nbsp;that the postback occured as a result of the button click to send the mail or if the postback occurred because of another action within the webform.</p> <p>I believe -&nbsp;and I have tested this idea too - that the button actually doesn't register as a post back but rather , it causes a submit action.</p> <p>Visually&nbsp;the submit action&nbsp;the same effect as a postback in so much as my contact form closed because the page state returned to the orignal onload state. To stop that&nbsp;I added script function to reopen the contact page, display thank you to the user and then gracefully close. &nbsp;</p> <p>All that works fine, but because the button does not register a postback event my way of trying to trap exactly what caused the postback (I wanted to be certain that the thank you script only ran on the mail send button click) I cannot test the sender that way. If you are interested in seeing what I mean by the form contact action you can click my website in my profile and it will take you to the page concerned where you can see the contact form working (click contact at the top of the page)&nbsp;.</p> <p>So at least&nbsp;I now know&nbsp;why I am not collecting the session variable value on the ispostback - simply because there is no postback. Ho hum. thank you for your time and energy. kind regards</p> <p>Raymond</p> <p>&nbsp;</p> 2012-05-14T10:56:34-04:004979914http://forums.asp.net/p/1800757/4979914.aspx/1?Re+session+variable+viewstare+variable+and+hidden+field+return+null+valuesRe: session variable, viewstare variable and hidden field return null values <p>Hi Mamba, again.</p> <p>Thanks to your example&nbsp;I&nbsp;Solved it.&nbsp;Once I stopped worrying, lol, I placed this example code on every stage of the page life cycle:</p> <p>If <span face="Consolas" size="2" style="font-family:Consolas; font-size:small"> <span face="Consolas" size="2" style="font-family:Consolas; font-size:small">IsPostBack </span></span><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small">Then</span></span></span></p> <p><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small">If</span></span></span><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"> Session(</span></span><span face="Consolas" color="#800000" size="2" style="color:#800000; font-family:Consolas; font-size:small"><span face="Consolas" color="#800000" size="2" style="color:#800000; font-family:Consolas; font-size:small"><span face="Consolas" color="#800000" size="2" style="color:#800000; font-family:Consolas; font-size:small">&quot;name&quot;</span></span></span><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"><span face="Consolas" size="2" style="font-family:Consolas; font-size:small">) </span></span><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small">IsNot</span></span></span><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"> </span></span><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small">Nothing</span></span></span><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"> </span></span><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small">Then</span></span></span></p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TextBox2.Text = Session(<span face="Consolas" color="#800000" size="2" style="color:#800000; font-family:Consolas; font-size:small"><span face="Consolas" color="#800000" size="2" style="color:#800000; font-family:Consolas; font-size:small"><span face="Consolas" color="#800000" size="2" style="color:#800000; font-family:Consolas; font-size:small">&quot;name&quot;</span></span></span><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"><span face="Consolas" size="2" style="font-family:Consolas; font-size:small">)</span></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p> <p><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small">End</span></span></span><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"> </span></span><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small">If</span></span></span>&nbsp;</p> <p>Label2.text = Session(<span face="Consolas" color="#800000" size="2" style="color:#800000; font-family:Consolas; font-size:small"><span face="Consolas" color="#800000" size="2" style="color:#800000; font-family:Consolas; font-size:small"><span face="Consolas" color="#800000" size="2" style="color:#800000; font-family:Consolas; font-size:small">&quot;name&quot;</span></span></span><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"><span face="Consolas" size="2" style="font-family:Consolas; font-size:small">) &#43; </span></span><span face="Consolas" color="#800000" size="2" style="color:#800000; font-family:Consolas; font-size:small"><span face="Consolas" color="#800000" size="2" style="color:#800000; font-family:Consolas; font-size:small"><span face="Consolas" color="#800000" size="2" style="color:#800000; font-family:Consolas; font-size:small">&quot; page Init&quot;</span></span></span></p> <p><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small">End</span></span></span><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"> </span></span><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small">If</span></span></span></p> <p>The save session obviously returned a value and the prerender returned a value but the other stages returned only the &#43; value I added, the session value was not returned although we know that it exists - so I placed my script call in the prerender stage of the page cycle and achieved what I wanted.</p> <p>I still have to go away and read to understand why after a button click the prerender accepts a postback argument, but that the init, initcomplete, preload and the load stages of the page cycle do not? I( did read somewhere that a button doesnt actually cause a postback, rather that it causes a submit action which in the life cycle of a page is interpretated somewhat differently?). Still I am learning. Thanks for your help. Hope this might help someone else sometime.</p> <p><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small"></span></span></span></p> <p><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" color="#0000ff" size="2" style="color:#0000ff; font-family:Consolas; font-size:small"></span></span></span></p> 2012-05-14T13:15:15-04:004980718http://forums.asp.net/p/1800757/4980718.aspx/1?Re+session+variable+viewstare+variable+and+hidden+field+return+null+valuesRe: session variable, viewstare variable and hidden field return null values <p></p> <blockquote><span class="icon-blockquote"></span> <h4>RayH1955</h4> I placed this example code on every stage of the page life cycle</blockquote> <p></p> <p>Well, I don't recommend this. Maybe you can post you full source code, and I will do some research for it to find what cause leads to session to null.</p> 2012-05-15T02:59:43-04:004980973http://forums.asp.net/p/1800757/4980973.aspx/1?Re+session+variable+viewstare+variable+and+hidden+field+return+null+valuesRe: session variable, viewstare variable and hidden field return null values <pre class="prettyprint">Hi Mamba</pre> <pre class="prettyprint">What I did as a test was to take your code and add a few more simple functions on the savesession, prerender, init etc stages of the page life cycle as per below and then ran it. When a stage returned a value into textbox 2 I remmed it out until I reached a part where the session variable was no longer returned which on this test was the init stage.</pre> <pre class="prettyprint">life </pre> <pre class="prettyprint">&nbsp;</pre> <pre class="prettyprint">Partial Class sessiontest Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Session("name") = TextBox1.Text End Sub Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click If Session("name") IsNot Nothing Then Label1.Text = Session("name") End If End Sub Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click ViewState("name") = TextBox1.Text End Sub Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click If ViewState("name") IsNot Nothing Then Label1.Text = ViewState("name") End If End Sub Protected Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button5.Click HiddenField1.Value = TextBox1.Text End Sub Protected Sub Button6_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button6.Click If HiddenField1 IsNot Nothing Then Label1.Text = HiddenField1.Value End If End Sub Protected Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init If IsPostBack Then If Session("name") IsNot Nothing Then TextBox2.Text = Session("name") End If Label2.Text = Session("name") + " page Init" End If End Sub Protected Sub Page_InitComplete(sender As Object, e As EventArgs) Handles Me.InitComplete If IsPostBack Then If Session("name") IsNot Nothing Then TextBox2.Text = Session("name") End If Label2.Text = Session("name") + " initcomplete" End If End Sub Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load If IsPostBack Then If Session("name") IsNot Nothing Then TextBox2.Text = Session("name") End If Label1.Text = Session("name") + " load" End If End Sub Protected Sub Page_PreRender(sender As Object, e As EventArgs) Handles Me.PreRender If IsPostBack Then If Session("name") IsNot Nothing Then TextBox2.Text = Session("name") End If Label2.Text = Session("name") + " prerender" End If End Sub Protected Sub Page_SaveStateComplete(sender As Object, e As EventArgs) Handles Me.SaveStateComplete If IsPostBack Then If Session("name") IsNot Nothing Then TextBox2.Text = Session("name") + " savestate" End If Label2.Text = Session("name") + " savestate" End If End Sub End Class</pre> <p></p> 2012-05-15T06:23:21-04:004983047http://forums.asp.net/p/1800757/4983047.aspx/1?Re+session+variable+viewstare+variable+and+hidden+field+return+null+valuesRe: session variable, viewstare variable and hidden field return null values <p>Hi,</p> <p>If all above are your full source code. Then just my sample code is enough. Don't need to add Init, InitComplete and PreRender event on your code behind. Now I suggest you to create new web site project and create a simple web page with my sample code to test again whether the session still is null.</p> 2012-05-16T08:01:32-04:004983175http://forums.asp.net/p/1800757/4983175.aspx/1?Re+session+variable+viewstare+variable+and+hidden+field+return+null+valuesRe: session variable, viewstare variable and hidden field return null values <p>Hi Mamba</p> <p>Thanks for your reply. The code I sent, based on yours with my additions,&nbsp;was my test. Maybe my explanation was a bit confusing. I added the page load events to see where within the page load events the session stopped being picked up as that was the point of my initial question- your example works well until you try to pick up a value within a page load cycle. If you run the code I added you will see what I mean. The page load events only allow me to collect the session variable value upto the pre render stage, after that for some reason&nbsp;using a 'ispostback' check fails to retrieve the session variable although of course it is&nbsp;still active. As I said, the code I supplied does I think illustrate that.</p> <p>It seems to me that&nbsp;&nbsp;picking up a session variable during a page load life cycle is only possible at the pre render stage&nbsp;&nbsp;- after that stage even your example failed to return the variable&nbsp;value that&nbsp;was set via your button click routimes. Either the code I was using to trap the value - example being anyone of the functions I added - is flawed or that the page does not allow the session variable to be grabbed as a variable to 'question' after the prerender stage. As you will notice after the prerender stage textbox 2 and the lbel 2 fail to&nbsp;display the session value 'name'.&nbsp;It is this confusion that I was trying to understand. Thanks for your time</p> 2012-05-16T09:04:02-04:004984431http://forums.asp.net/p/1800757/4984431.aspx/1?Re+session+variable+viewstare+variable+and+hidden+field+return+null+valuesRe: session variable, viewstare variable and hidden field return null values <p>Hi,</p> <p>Sorry I'm still confused about your comments.</p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>RayH1955</h4> As you will notice after the prerender stage textbox 2 and the lbel 2 fail to&nbsp;display the session value 'name'.&nbsp;It is this confusion that I was trying to understand. </blockquote> <p></p> <p>You mean you couldn't get the session variable at the SaveStateComplete stage? I test this, and I can get the session variable:</p> <pre class="prettyprint">Partial Class Default4 Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Session(&quot;name&quot;) = TextBox1.Text End Sub Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click If Session(&quot;name&quot;) IsNot Nothing Then Label1.Text = Session(&quot;name&quot;) End If End Sub Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click ViewState(&quot;name&quot;) = TextBox1.Text End Sub Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click If ViewState(&quot;name&quot;) IsNot Nothing Then Label1.Text = ViewState(&quot;name&quot;) End If End Sub Protected Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button5.Click HiddenField1.Value = TextBox1.Text End Sub Protected Sub Button6_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button6.Click If HiddenField1 IsNot Nothing Then Label1.Text = HiddenField1.Value End If End Sub Protected Sub Page_SaveStateComplete(ByVal sender As Object, ByVal e As EventArgs) Handles Me.SaveStateComplete If IsPostBack Then If Session(&quot;name&quot;) IsNot Nothing Then TextBox1.Text = Session(&quot;name&quot;) &#43; &quot; savestate&quot; End If Label1.Text = Session(&quot;name&quot;) &#43; &quot; savestate&quot; End If End Sub End Class</pre> <p></p> 2012-05-17T03:37:46-04:004984956http://forums.asp.net/p/1800757/4984956.aspx/1?Re+session+variable+viewstare+variable+and+hidden+field+return+null+valuesRe: session variable, viewstare variable and hidden field return null values <p>Thank you for your patience and example above.</p> <p>This sort of takes me back to my opening question. Your examples above work of course - and so do all the stages of the page life cycle and I can return the session var BUT I can't do that within my original project, I can only acheive this state of 'yep it works' if I create a new project and&nbsp;place your examples within it.</p> <p>My orignal project which only contained 1 page, a page I created to test a mail function, does not return a request to display a session varible on the 'page load', it will only do so at the savesessionstate and the prerender states, after that time it&nbsp;returns null BUT when I set up a simple second page linked from the first and&nbsp;call the session var on open/load that page displays the session var. What I could not do within that project was call the session state on&nbsp;my original send mail test page.&nbsp; That particular page did have a content update panel and I thought that might be the cause so I removed it but still I could not retrieve the session var other then through the 2 states I mentioned. Hence my confusion and question. I was looking for a clue as to why that might be.</p> <p>code wise the page could not be more simple, it declares the to and from vars for the mail, and then has the mail method and that functions fine.&nbsp; Once the mail is sent a script call fires a simple jQuery function to keep the mail page open (it is a pop up idea) and to write thank you in the comment box and then using a timer the comment section then&nbsp;fades out.</p> <p>I know that code isn't conflicting because I unhocked that too.</p> <p>The objective for me was to set a session var&nbsp;so that if the page was developed further and I had more causes for the page to post back I could trap where the postback occurred and if it&nbsp;did not occur from the mail send&nbsp;function&nbsp;then I&nbsp; could prevent&nbsp;the jquery script&nbsp;part from 'automatically' firing.</p> <p>so at the mail send function I was setting the session&nbsp;=&nbsp;'mail'. then on the page_load I included the if ispostback then if session=&quot;mail&quot; run my jQuery script else don't run it.</p> <p>but on this page the session var always returns null&nbsp; unless I place the if postback function within the pages prerender call., then the logic triggers and the jQuery script is run (or not if required)</p> <p>I then took your original example and set that up within the project with the same result - being I could not return the session var after the prerender stage of the page life cycle. So having retried your example in a new empty project and found that&nbsp; a call to the session works right upto the page_load stage then clearly something within the original&nbsp;project was causing the 'weird'&nbsp;behaviour which had nothing to do with coding within a page - your example could not be more simple&nbsp;but within the project that&nbsp;failed also - so there must be something within the project headers or somewhere which is causing the problem.</p> <p>My original question was I could not figure out the why this particualr page / project&nbsp;should be&nbsp;acting in that way and I still don't know. &nbsp;</p> <p>But I really do thank you for your patience and assistance. At least I know now that without doubt the session does work in the way 'stated' and if it doesn't I need to be looking at the project environment. Just what though is the question.</p> <p>thanks again</p> <p>&nbsp;</p> 2012-05-17T09:29:47-04:004986271http://forums.asp.net/p/1800757/4986271.aspx/1?Re+session+variable+viewstare+variable+and+hidden+field+return+null+valuesRe: session variable, viewstare variable and hidden field return null values <p>Hi,</p> <p>Well,</p> <p>Thanks for your following up.</p> <p>Because I can't view your real full souce code. So I only provide my simple sample code for you to test. It's doesn't work for your original project. Very weird.</p> <p>Now some tips for you.</p> <p>Try debuging your page step by step. You will view each step your source code executes. That may help you to determine when the session set and get. If the session variable you set correct before get it. It should return some value not null. So please debug your page step by step to determine whether you set the Session variable. IsPostback g<span style="">ets a value that indicates whether the page is being rendered for the first time or is being loaded in response to a postback. The value is true <span style="">if the page is being loaded in response to a client postback; otherwise,<span class="Apple-converted-space">&nbsp;false.</span></span></span></p> <p><span style=""><span style=""><span class="Apple-converted-space">BTW, if possible you can remove most of source code just some logic source code and post it here. I'll view it carefully.</span></span></span></p> 2012-05-18T04:06:23-04:00