Redirect and Target="_blank"

  • Thread starter msnews.microsoft.com
  • Start date
M

msnews.microsoft.com

I want to redirect the user to a url outside of our website but I want it to
preserve our application's window by opening a new window. We have a
datagrid that has five hyperlink columns containing links to external sites.
The hyperlink columns have the target="_blank" attribute so they preserve
the existing window. This works OK except that the NavigateURL's are quite
long due to the size of the query string and this makes the page slow. What
I've done is I've changed the hyperlink columns to template columns with a
linkbutton control. Now when a link is clicked I form the URL with the long
querystring (using the command argument and other values from some dropdown
list controls) and do a redirect to that URL:

Response.Redirect(url, False)

This makes the page much faster because it does not have those long URL's,
but the external links on the page do not open in a new window as before.
Is there a method I can use that will duplicate the Target="_blank" behavior
and preserve our application window?

Thanks,
Al
 
C

Captain Chaos

Maybe there is a better solution but anyway:

Create a JavaScript on the Serverside (in your Code)
and send it back to the Client where it the is executed.

It seams that you do a serverroundtrip anyway with your solution.


Instead of:
Response.Redirect(url, False)

write:

Response.write "<script language='JavaScript'>" & vbcrlf
Response.write "<!--" & vbcrlf
Response.write "window.open( )" & vbcrlf
Response.write "//-->" & vbcrlf
Response.write "</script>" & vbcrlf


But I think if you looking to design a fast site the solution with the
serverroundtrips isn't very fast.

Maybe you can make the long expression shorter.
May be there are parts in the URL that are always the same.
So take this parts into JavaScript Variables and put
the real URL then at runtime.

Cherrio
 
A

Ather Ali Shaikh

you can do it by using Item_databound in grid control that
find the linkbutton and add an attributelike

myLinkButton.Attributes.Add("Target", "_blank")

regards
Ather Ali Shaikh
 
K

Kevin Spencer

You must have a monster of a page if a bunch of URLs are slowing it down! In
any case, if you want to keep the URLs out of the page, first you have to
understand first that there are only 2 ways to launch a new instance of your
browser: (1) using target frame, and (2) using the JavaScript window.open()
method. Obviously, the target frame solution doesn't work, as it requires
the URL to be in the page at the time the link is clicked. So, if I
understand you correctly, your requirement is that the URLs are not in the
Page, but that an event such as a click must open in a new Window with a
dynamically-generated URL, your path is pretty-well set.

First, in order to do any navigation, you will need a URL. Since your
requirement is that no URLs appear in the page, this means that you have to
do a PostBack to fetch the appropriate one. You can choose whatever type of
Control you desire into your DataGrid to initiate the PostBack; the
important thing is that you write a server-side event handler for the click
event of that Control. The server-side event handler would then fetch the
URL for that Control, and use Page.RegisterStartupScript to add a JavaScript
to the Page which runs when the Page reloads. That JavaScript would use the
window.open() method to open the URL in a new browser instance.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
M

msnews.microsoft.com

Dear Captain Chaos,

Thanks for your suggestions. I'm going to try your solution.

Al
 
M

msnews.microsoft.com

I'll try it. Thanks.

Ather Ali Shaikh said:
you can do it by using Item_databound in grid control that
find the linkbutton and add an attributelike

myLinkButton.Attributes.Add("Target", "_blank")

regards
Ather Ali Shaikh

it
 
M

msnews.microsoft.com

Yes, it is a monster. When I remove the hyperlinks, it's much much faster.
I think you grasped the problem correctly. Thanks for your detailed
explanation. I'm going to try your solution. I did not know about the
Page.RegisterStartupScript method.

Thanks,
Al
 
M

msnews.microsoft.com

Kevin,

Would the window.open() be inside of a function in the JavaScript?

Or could it be like this:

' Form the script to be registered at client side.
Dim scriptString As String = "<script language=JavaScript>
window.open(); "
scriptString += "<"
scriptString += "/"
scriptString += "script>"

If (Not Me.IsStartupScriptRegistered("Startup")) Then
Me.RegisterStartupScript("Startup", scriptString)
End If

I put the above code in the event handler of the linkButton Command event.
It didn't have any effect. I stepped through it so I know that it is
executing.

Thanks,
Al
 
A

Al Cadalzo

Ather Ali Shaikh,

I added the attribute but it doesn't seem to have had any effect.

Al
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top