Text box shows values incorrectly after return to page (ASP.NET 3.5)
<p>could you please clarify my issue?</p>

<p>I have a starting page ListBox1.aspx that calls another one ListBox1_sub.aspx. I need to preserve a state of the page ListBox1.aspx so that after returning from ListBox1_sub.aspx all the controls of ListBox1.aspx keep their state. I assume it could be achieved by using a client-side code and in the attached code everything looks OK except one thing.</p>

<p>In my code, I set the value of the Session variable Session("back_from_ListBox1_sub") in ListBox1_sub.aspx. After returning from ListBox1_sub.aspx to ListBox1.aspx, I expected the txtReturn TextBox to show the value of the Session variable. However, although this value was set to the text box, it was not shown.</p>

<p>In the below code, the value of the Session variable is displayed only after the 2nd call to ListBox1_sub.aspx. For example, if I set the value to "x" in the first call and to "y" in the second call, after returning from the 2d call the value will be set to "x".</p>

<p>Can you advise as to how I should modify my code so that the returned value be displayed correctly in the first page after the call to the second page?
Many Thanks,
Lev</p>

<p>======= test code =======
== ListBox1.aspx =====</p>

<pre><code>&lt;%@ Page Language="VB" AutoEventWireup="false" CodeFile="ListBox1.aspx.vb" Inherits="ListBox1" %&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head runat="server"&gt;
    &lt;title&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;form id="form1" runat="server"&gt;
    &lt;div&gt;
    &lt;asp:CheckBoxList ID="CheckBoxList1" runat=server Visible="True"&gt;
    &lt;asp:ListItem Text="a" /&gt;
    &lt;asp:ListItem Text="b" /&gt;
    &lt;asp:ListItem Text="c" /&gt;
    &lt;asp:ListItem Text="d" /&gt;
    &lt;/asp:CheckBoxList&gt;
     &lt;br /&gt;
    &lt;asp:LinkButton ID="lnkWindowOpen" runat="server" Text="Window.Open"&gt;
    &lt;/asp:LinkButton&gt;
    &lt;br /&gt;
    &lt;asp:TextBox ID="txtReturn" runat="server"&gt;
    &lt;/asp:TextBox&gt;
    &lt;/div&gt;
    &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>'code-behind</p>

<pre><code>Partial Class ListBox1
    Inherits System.Web.UI.Page

    Public Const JS_START As String = "&lt;script language=javascript&gt;"
    Public Const JS_END As String = "&lt;/script&gt;"
    Public Const DBQT As Char = Chr(34)

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not (Session("back_from_ListBox1_sub") Is Nothing) Then
            txtReturn.Text = Session("back_from_ListBox1_sub").ToString
        End If
    End Sub

    Protected Sub lnkWindowOpen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkWindowOpen.Click
        Dim message As String
        Dim script As String
        message = ""
        message += "window.blur();"
        message += "window.open(" &amp; DBQT &amp; "ListBox1_sub.aspx" &amp; DBQT &amp; ",null,"
        message += DBQT &amp; "height=200,width=400,top=300,left=325,"
        message += "status=yes,toolbar=no,titlebar=0,menubar=no,location=no,scrollbars=0,resizable=0;modal=yes" &amp; DBQT &amp; ");"
        script = JS_START &amp; message &amp; JS_END
        ClientScript.RegisterClientScriptBlock(Page.GetType, Guid.NewGuid().ToString(), script)
    End Sub

End Class
</code></pre>

<p>======== page ListBox1_sub.aspx ==========  </p>

<pre><code>&lt;%@ Page Language="VB" AutoEventWireup="false" CodeFile="ListBox1_sub.aspx.vb" Inherits="ListBox1_sub" %&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head runat="server"&gt;
    &lt;title&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;form id="form1" runat="server"&gt;
    &lt;div&gt;
    &lt;asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"&gt;
    &lt;asp:ListItem Text="x" /&gt;
    &lt;asp:ListItem Text="y" /&gt;
    &lt;asp:ListItem Text="z" /&gt;
    &lt;/asp:ListBox&gt;
    &lt;br /&gt;
    &lt;asp:TextBox ID="TextBox1" runat="server"&gt;&lt;/asp:TextBox&gt;
    &lt;br /&gt;
    &lt;asp:LinkButton ID="lnkBack" runat="server"&gt;Back&lt;/asp:LinkButton&gt;
    &lt;/div&gt;
    &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>'code-behind</p>

<pre><code>Partial Class ListBox1_sub
    Inherits System.Web.UI.Page

    Public Const JS_START As String = "&lt;script language=javascript&gt;"
    Public Const JS_END As String = "&lt;/script&gt;"
    Public Const DBQT As Char = Chr(34)

    Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        TextBox1.Text = ListBox1.SelectedItem.Text
    End Sub

    Protected Sub lnkBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkBack.Click
        Dim message As String
        Dim script As String
        Session("back_from_ListBox1_sub") = TextBox1.Text
        message = "window.close();"
        script = JS_START &amp; message &amp; JS_END
        ClientScript.RegisterClientScriptBlock(Page.GetType, Guid.NewGuid().ToString(), script)
    End Sub

End Class
</code></pre>
