Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

Opening a new Window from ASP.NET code-behind

  Asked By: Doan    Date: Apr 07    Category: Asp.net    Views: 10372
  

I'm trying to figure out how to open a new window in asp.net from the
code-behind, VB, specifically, launch a new window after a selection
is made from a dropdownlist server control. I know about the
OnSelectedIndexChanged property, but don't know the syntax to convert
the jscript


"window.open
("TestPage.aspx",null,"height=200,width=400,status=yes,toolbar=no,menu
bar=no,location=no");"

to equivalent vb.net code. Any ideas?

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Adelinda Fischer     Answered On: Apr 07

All your server  code does it send it to the browser.

To open  a new window  that side you HAVE to tell it to do it from javascript.

There are ways to do it so it "looks" like server code  is doing it.

You can write a server control  that outputs the required javascript along with the url.

Or you can Add attributes to a server control or an existing "off the shelf" control.

But all in all - IT HAS TO BE JAVASCRIPT that does the deed.

At teh end of the day you might as well turn your script below into a function and manually put it at teh top of you aspx  page - calling via hrefs.


<script>
function openit(mypage){
"window.open
(mypage,null,"height=200,width=400,status=yes,toolbar=no,menu
bar=no,location=no");"
}
</script>

called via hrefs like
<a href=javascript:void(0) onclick=openit("anypage.aspx")>OpenIt</a>
or
<a href=javascript:openit("anypage.aspx")>OpenIt</a>

Where the href itself can be genearted from server code via a Linklabel or a Label or something.

At the end of the day .... if you do supply the javascript function along with every href you throw out you will have reprodctions of it scattered throughout your code. If you don't want that then you'll have to tag the first instance it is outputted and then don't ouput it again.

 
Answer #2    Answered By: Tamara Nguyen     Answered On: Apr 07

You can accomplish this by adding an attribute to your DropDownList.
This will add a Javascript event handler to your rendered HTML and
allow you to easily open  a new window:

In the Page_Load event add this code


DropDownList1.Attributes.Add("onchange", "window.open
('TestPage.aspx','myNewWindow','height=200,width=400,status=yes,toolbar=no,menu
bar=no,location=no'))";

When your page is loaded this attribute will be added to the
DropDownList tag and when you click a new item in the list
the window  will open.

 
Answer #3    Answered By: Ujala Hashmi     Answered On: Apr 07

I knew I could add an attribute, but I didn't know it was the "onchange" one for
a dropdown list. I'll try it soon.

 
Answer #4    Answered By: Robin Bailey     Answered On: Apr 07

In your codebehind, you should inser some client javascript to Response... Or to
same placeholder that will execute when the page gets loaded on the client...

 
Didn't find what you were looking for? Find more on Opening a new Window from ASP.NET code-behind Or get search suggestion and latest updates.




Tagged: