Transferring a value to a popup page

esposito

Centurion
Joined
Jul 11, 2003
Messages
103
Location
Perugia - Italy
Hello, I would like to open a popup page in ASP.NET (VB.NET) transporting a value from the calling form to the popup page.

Now, to tranfer a value from a page to another, first I declare the classes for the values to be exported and then I use the following code:

Visual Basic:
Server.Transfer ("newwin.aspx?AccessKey=" & "mykey")

To get a popup page, I use the following Javascript:

Visual Basic:
Sub btnPopup_Click(sender As Object, e As EventArgs)

    Dim sScript As String
    sScript = "<script>window.open('newwin.aspx','','height=100','width=100','scrollbars=no');<" & "/script>"
    Response.Write(sScript)

End Sub

What I can't do is get both functions at the same time, i.e. I can't export a value to a popup page.

Any help will be appreciated.
 
You can solve this problem in two ways (maybe not the best, but they work):

1) declare a session or viewstate variable:

Visual Basic:
Session("accesskey") = "mykey"
'---or---
ViewState("accesskey") = "mykey"

'Then in the popup
dim mykey as string = Session("accesskey")
'---or---
dim mykey as string = ViewState("accesskey")

2) Just code it into your popup script:
Visual Basic:
Response.Write("<script language=""javascript"">window.open('newwin.aspx?accesskey=" & myKey & "','','height=100','width=100','scrollbars=no');</script>")

Hope that helps! Good Luck, Brian
 
I tried to use the second method you suggested (embedding the parameter in the javascript code). All I got was a popup window showing an error message (the resource cannot be found).

Probably that javascript code is not a good substitute for the Response.Transfer method.

Unfortunately, I did not suceed in implementing the first method either. I'm afraid I would need more info on how to proceed.

Question: is there any way I could put the javascript code in the Page_Load event of a .aspx page to open it as a popup page? In that way, I could use normal VB.NET code in the calling form.

TIA
 
You're not going to be able to open a new window from code behind - since codebehind is executing on the server. You'd need to do it client side - or you need to have your codebehind generate client-side script, as cyclonebri illustrated, which is ideal if you need to build up a query string as part of your URL, as cyclonebri illustrated. A query string is a good way to pass a value unless you need to keep that value hidden, in which case session is a better idea (although neither are secure without encryption).

The javascript window.open should work. You would of course need to set the URL that's being "popped" to something valid. If you see a new window but get "the resource cannot be found" it means your popup script is working fine but the URL you're opening is invalid.

And, if you wanted to do anything with the value you're passing you'd need to properly retrieve it as a query string value.

Paul
 
Back
Top