open.window from codebehind

md0333
md0333 used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2011

Commented:
Do you have masterpage in your solution?
Top Expert 2011

Commented:
put javascript code in aspx

and change
        ClientScript.RegisterStartupScript(Me.GetType(), "openwin", openPopUp2('" & url & "')")

Author

Commented:
Not at this point of the application.
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

Top Expert 2011

Commented:
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)
Top Expert 2011

Commented:
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);}

Author

Commented:
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...

Top Expert 2011

Commented:
Look here:
Opening a PopUp new window with form authentication ticket
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_21751657.html

Author

Commented:
added FormsAuthentication.SetAuthCookie(sUserName, False)

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

Top Expert 2011

Commented:
please  test
on The onClick attribute is
ButtonLogin.Attributes.Add("onclick", "javascript:window.open('Your.aspx'); return false;")
Top Expert 2011

Commented:
ButtonLogin.Attributes.Add("onclick", "javascript:window.open('Your.aspx'); return true;")

Author

Commented:
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.
Carlos VillegasFull Stack .NET Developer

Commented:
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

Author

Commented:
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.
Carlos VillegasFull Stack .NET Developer

Commented:
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


Author

Commented:
@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.

Author

Commented:
I have put an alert in the javascript also and that never gets hit... so, it's never running the javascript code.
Carlos VillegasFull Stack .NET Developer

Commented:
Ok md0333, I will help you with that, let me do some tests...
Full Stack .NET Developer
Commented:
Hi buddy, I see that your problem is by design, the login control always do a redirect when the user is authenticated, so that make useless the call to ScriptManager.RegisterStartupScript, I did another example based in your scenario, maybe you can do something like that, please see the attached demo web site:
DemoLoginWebSite.zip

The demo users inside the web config are:
User   Password
Bob       123
Alice      123

Start the Login.aspx page and try to authenticate the user Bob, that must open Page1.aspx in a new window, for Alice is Page2.aspx.

Give a try, I hope that will give you an idea of how to work aground that, or maybe implement this example in your project.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial