Avatar of md0333
md0333Flag for United States of America

asked on 

open.window from codebehind

I've seen several solutions to doing this but I have yet to get one to work...

I have a website that I'm trying to get (from login) to open a web application in a new window. Once the user logs in and gets validated I set some session variables and then choose the page the user goes to based on their security level. If I use the response.redirect() then it works fine... just doesn't open in a new window but my point is that I know the user is being validated. But if I try to call the javascript (or build the javascript) from codebehind it immediately takes the app to the logout screen.

Here is the last piece of code I tried
        Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()
        sb.Append("<script language='javascript'>")
        sb.Append("window.open('webapp.aspx', '', 'resizable=yes, menubar=no, toolbar=no,   status=no, location=no, channelmode=yes')<")
        sb.Append("/script>")

        If Not ClientScript.IsClientScriptBlockRegistered(Me.GetType(), "PopupScript") Then
            ClientScript.RegisterClientScriptBlock(Me.GetType(), "openPopUp", sb.ToString())
            'ClientScript.RegisterStartupScript(Me.GetType(), "openPopUp", sb.ToString())
            'Response.Write(sb.ToString())
        End If

Open in new window



you can see I have tried a couple different methods... I've tried several others also including this one.
codebehind
Dim url As String = "~/webapp.aspx"
        ClientScript.RegisterStartupScript(Me.GetType(), "openwin", "<script>openPopUp2('" & url & "')</script>")

aspx
function openPopUp2(url) {
            alert('hi');
            window.open(url, "", " resizable=yes, menubar=no, toolbar=no, status=no, location=no, channelmode=yes");
        }

Open in new window


I put the alert in there just to see if it was even hitting the javascript code and it is not... not sure what to try next.

thanks
ASP.NETVisual Basic.NETWeb Development

Avatar of undefined
Last Comment
Carlos Villegas
Avatar of dj_alik
dj_alik

Do you have masterpage in your solution?
Avatar of dj_alik
dj_alik

put javascript code in aspx

and change
        ClientScript.RegisterStartupScript(Me.GetType(), "openwin", openPopUp2('" & url & "')")
Avatar of md0333
md0333
Flag of United States of America image

ASKER

Not at this point of the application.
Avatar of dj_alik
dj_alik

javascript
 function OpenPopup(){window.open("'webapp.aspx?", "_blank", "height=500, width=575, left=150,top=150, " +"location=no, menubar=no, resizable=no, " +"scrollbars=no, titlebar=no, toolbar=no", true);}

  ClientScript.RegisterStartupScript(Me.GetType(), "openwin", "OpenPopup();",True)
Avatar of dj_alik
dj_alik

prepare javascript
in aspx
 function OpenPopup(){window.open("webapp.aspx?", "_blank", "height=500, width=575, left=150,top=150, " +"location=no, menubar=no, resizable=no, " +"scrollbars=no, titlebar=no, toolbar=no", true);}
Avatar of md0333
md0333
Flag of United States of America image

ASKER

None of those worked...

I've read a couple of blogs that talked about problems with session variables when opening popup windows.

If I use window.open to a page and then login  from that window, I don't have an issue. But when I'm trying to login and use the window.open at same time I'm having the issue. Not sure if I'm on to something but I figured I'd throw it out there...

Again, I put an alert in the javascript code and it is not hitting the alert... just going to the logout page. Which it would do if the session had ended...

Avatar of md0333
md0333
Flag of United States of America image

ASKER

added FormsAuthentication.SetAuthCookie(sUserName, False)

and tried all the different ways... still not hitting the javascript. It's immediately going to the logout page.

Avatar of dj_alik
dj_alik

please  test
on The onClick attribute is
ButtonLogin.Attributes.Add("onclick", "javascript:window.open('Your.aspx'); return false;")
Avatar of dj_alik
dj_alik

ButtonLogin.Attributes.Add("onclick", "javascript:window.open('Your.aspx'); return true;")
Avatar of md0333
md0333
Flag of United States of America image

ASKER

did not work...

OK. Even if I put an alert directly in the ScriptManager.RegisterClientScriptBlock or ClientScript.RegisterClientScriptBlock it still does not fire.

ScriptManager.RegisterClientScriptBlock(Page, GetType(Page), "OpenWindow", "<script>alert('Hello');</script>", True)

I'm thinking this has to do more with authentication then anything else.

web.config
<authentication mode="Forms">
      <forms name=".ASPXAUTH" loginUrl="Logout.aspx" protection="All" timeout="60" path="/" requireSSL="false" slidingExpiration="true" cookieless="UseDeviceProfile" />
</authentication>

The Logout.aspx page is what I keep getting when trying to use the javascript to open a new window.

Again, if I use response.redirect everything works... it just does not open in a new window. If I put a button on the first page and run the javascript on client side to open a new window and then from that new page (already a window) I login in then everything works fine. I'm just trying to avoid the additional page... Open the window from the login. It just seems that that as soon as I use the Clientscript or Scriptmanager it just goes straight to Logout.aspx.

It seems as if the web.config file is not recognizing the new session and sending the user straight to logout.aspx... I'm just throwing things out there hoping to get some ideas.

Thank you.
Hi, I see some errors using the ScriptManager class in the posts above, please try this example:
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        ' Asuming that your user have valid credentials...
        FormsAuthentication.SetAuthCookie("MyUserName", False)
        
        ' Then this will call your openPopUp2 javascript function when the page is loaded, so webapp.aspx will be open in a new window.
        ' Note that a popup blocker can block this action.
        Dim myUrl As String = "webapp.aspx"
        ScriptManager.RegisterStartupScript(Me, Me.GetType(), "openPopUp2", "openPopUp2('" + myUrl + "');", True)
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function openPopUp2(url) {
            window.open(url, "", "resizable=yes, menubar=no, toolbar=no, status=no, location=no, channelmode=yes");
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Validate User" />
    </div>
    </form>
</body>
</html>

Open in new window


This is the aspx file:  OpenNewWindow.aspx
Avatar of md0333
md0333
Flag of United States of America image

ASKER

Still does not work.

Again, I am using the built in ASP authentication and login controls. The codebehind is being called by the asp:Login control. I'm wondering if since that is a server side control whether this can be done.

<asp:Login ID="LoginUser" runat="server" EnableViewState="false" RenderOuterTable="false"
     DisplayRememberMe="false"  OnLoggedIn="loginRedirect" RememberMeSet="false" >

Open in new window


    Protected Sub loginRedirect(ByVal sender As Object, ByVal e As EventArgs)

        FormsAuthentication.SetAuthCookie(sUserName, False)
        Dim url As String = "mywebapp.aspx"
        ScriptManager.RegisterStartupScript(Me, Me.GetType(), "openPopUp2", "openPopUp2('" + url + "');", True) 'this goes straight to logout page

        Response.Redirect(url) 'this works... just doesn't open a new window

    End Sub

Open in new window


In this sub I actually check the users Role and send them to different pages based on those roles but I've removed all of that for readability.
Hi Buddy, your code look good, but why do you call Response.Redirect(url)???, based in your last post, what if you comment that line? try this:
    Protected Sub loginRedirect(ByVal sender As Object, ByVal e As EventArgs)
        FormsAuthentication.SetAuthCookie(sUserName, False)
        Dim url As String = "mywebapp.aspx"
        ScriptManager.RegisterStartupScript(Me, Me.GetType(), "openPopUp2", "openPopUp2('" + url + "');", True) 'this goes straight to logout page
    End Sub

Open in new window


Avatar of md0333
md0333
Flag of United States of America image

ASKER

@yv989c

I was just pointing out that the user DOES get authorized and that is not the problem because I can use Response.Redirect and it works fine.

I did try the code just as you have it and it goes straight to logout screen.
Avatar of md0333
md0333
Flag of United States of America image

ASKER

I have put an alert in the javascript also and that never gets hit... so, it's never running the javascript code.
Ok md0333, I will help you with that, let me do some tests...
ASKER CERTIFIED SOLUTION
Avatar of Carlos Villegas
Carlos Villegas
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo