could you please clarify my issue?
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.
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.
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”.
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
======= test code =======
== ListBox1.aspx =====
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="ListBox1.aspx.vb" Inherits="ListBox1" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBoxList ID="CheckBoxList1" runat=server Visible="True">
<asp:ListItem Text="a" />
<asp:ListItem Text="b" />
<asp:ListItem Text="c" />
<asp:ListItem Text="d" />
</asp:CheckBoxList>
<br />
<asp:LinkButton ID="lnkWindowOpen" runat="server" Text="Window.Open">
</asp:LinkButton>
<br />
<asp:TextBox ID="txtReturn" runat="server">
</asp:TextBox>
</div>
</form>
</body>
</html>
‘code-behind
Partial Class ListBox1
Inherits System.Web.UI.Page
Public Const JS_START As String = "<script language=javascript>"
Public Const JS_END As String = "</script>"
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(" & DBQT & "ListBox1_sub.aspx" & DBQT & ",null,"
message += DBQT & "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" & DBQT & ");"
script = JS_START & message & JS_END
ClientScript.RegisterClientScriptBlock(Page.GetType, Guid.NewGuid().ToString(), script)
End Sub
End Class
======== page ListBox1_sub.aspx ==========
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="ListBox1_sub.aspx.vb" Inherits="ListBox1_sub" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged">
<asp:ListItem Text="x" />
<asp:ListItem Text="y" />
<asp:ListItem Text="z" />
</asp:ListBox>
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:LinkButton ID="lnkBack" runat="server">Back</asp:LinkButton>
</div>
</form>
</body>
</html>
‘code-behind
Partial Class ListBox1_sub
Inherits System.Web.UI.Page
Public Const JS_START As String = "<script language=javascript>"
Public Const JS_END As String = "</script>"
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 & message & JS_END
ClientScript.RegisterClientScriptBlock(Page.GetType, Guid.NewGuid().ToString(), script)
End Sub
End Class
The reason your value isn’t showing is because the page that opens the pop-up isn’t refreshing to show the new value.
In your lnkBack_Click try setting message to this.
This will close the pop-up and tell the first page to reload.
EDIT: Update the field with javascript instead of re-loading
What we will do is pass the id of the field we want to update to the popup. When the user clicks the button we find the field on the opening page and update it. We no longer need to use session to pass the values. I am not at a PC where I can test the code, so it might not work as is. I don’t do any error checking, you will want to add this.
Partial Class ListBox1
Inherits System.Web.UI.Page
End Class
Partial Class ListBox1_sub
Inherits System.Web.UI.Page
End Class