Open new window with javascript from code behind file

What do I have to change to get the line in comment working ?

Private
Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
   Page.Validate()
   
If (Page.IsValid) Then
      
If Textbox1.Text <> "" Then
         
UpdateButton_Click(Button1, e)
      
Else
         
InsertButton_Click(Button1, e)
      
End If
      'Response.Write("<script>window.open('Child.aspx?queryString=' + document.getElementById('DropDownList1').value, 'Title');</script>")
   
Else
      
'ErrorMessage
   
End If
End Sub


Wie vroeg sterft, is langer dood
0
Joop_
5/30/2005 9:40:33 AM
asp.net.client-side 24353 articles. 2 followers. Follow

9 Replies
1399 Views

Similar Articles

[PageSpeed] 45
Get it on Google Play
Get it on Apple App Store

Proble you are facing is the code is being added to the top of the page, and the page is rendered from top to bottom so it fires before your element is created. PLUS a dropdown list does not have a value property like that. IE says it does, but other browsers do not.

There is two things you can do, you can use the RegisterClientScriptBlock which puts the code directly in front of the closing form tag.

Dim scriptString as String = "<script type='text/javascript'>" & _
"var sel = document.formName.selectName;" & _
"var selValue = sel.options[sel.selectedIndex].value;" & _
"var url = 'Child.aspx?queryString=' + selValue;" & _
"var winPop = window.open(url,'winPop');" & _
"</script>"
RegisterClientScriptBlock("clientScript", scriptString)

OR you can stick with your method and do this if you do not have any other onload tags on your page:

Dim scriptString as String = "<script type='text/javascript'>" & _
"window.onload = function(){" & _
"var sel = document.formName.selectName;" & _
"var selValue = sel.options[sel.selectedIndex].value;" & _
"var url = 'Child.aspx?queryString=' + selValue;" & _
"var winPop = window.open(url,'winPop');" & _
"}" & _
"</script>"
Response.Write(scriptString)



Hope that helps
Eric

Pascarello.com | Twitter epascarello | LinkedIn
0
A1ien51
5/30/2005 1:48:46 PM

A1ien51, I tried your first method, but I don't know how to call "clientScript".
Can you tell me?

But isn't it possible to do it my way, because the following line works:

Response.Write("<script>window.open('Child.aspx?queryString=2', 'Title');</script>")

And this one doesn't work :

Response.Write("<script>window.open('Child.aspx?queryString=' + document.getElementById('DropDownList1').value, 'Title');</script>")

And the difference isn't that big ....


Wie vroeg sterft, is langer dood
0
Joop_
5/30/2005 2:30:26 PM
You should just put that code on your code behind, clientscript just adds it to the part after your form, look at the documentation with VS and it will tell you about it.

Now lets see if I can explain why your way does not work in another way:

Lets say that you and I are going to a party. I am there early and I am yelling your name. Of course I will not get an answer since you are going to show up on time. So I leave since you are not there.

Now the document.getElementById('DropDownList1').value is me yelling your name. It yells once and does not hear a responce so it errors out. Hence why your code is not working. 
 
Now if I wait until the party starts and yell your name, you answer back. That is what you need to do.

You need to call your function either at the end of the document, or on the onload of the page. That is what the code I posted should do.

Eric


Pascarello.com | Twitter epascarello | LinkedIn
0
A1ien51
5/30/2005 4:35:06 PM
PLUS you have to change the formName and the elementName in my script!
Pascarello.com | Twitter epascarello | LinkedIn
0
A1ien51
5/30/2005 4:37:42 PM
 When I use this code, I get an error saying: 'sel.options' is empty or not an object ... 

Page.Validate()
If (Page.IsValid) Then
   
If Textbox0.Text <> "" Then
      
UpdateButton_Click(Button10, e)
   
Else
      
InsertButton_Click(Button10, e)
   
End If

   
Dim scriptString As String = "<script type='text/javascript'>" & _
                                                      "var sel = document.Form1.DropDownList10;" & _
                                                      "var selValue = sel.options[sel.selectedIndex].value;" & _
                                                      "var url = 'Winkel.aspx?queryString=' + selValue;" & _
                                                      "var winPop = window.open(url,'Leverancier');" & _
                                                      "</script>"
   RegisterClientScriptBlock("clientScript", scriptString)
Else
   
ErrorMessage....
End If


Wie vroeg sterft, is langer dood
0
Joop_
6/1/2005 9:28:02 AM
try this

  Dim scriptString As String = "<script type='text/javascript'>" & _
                                                      "window.onload = function(){var sel = document.Form1.DropDownList10;" & _
                                                      "var selValue = sel.options[sel.selectedIndex].value;" & _
                                                      "var url = 'Winkel.aspx?queryString=' + selValue;" & _
                                                      "var winPop = window.open(url,'Leverancier');}" & _
                                                      "</script>"
   RegisterClientScriptBlock("clientScript", scriptString)

if people have pop up blockers, this will be blocked, just a warning.

Eric

Pascarello.com | Twitter epascarello | LinkedIn
0
A1ien51
6/1/2005 1:28:16 PM

I am working with tabs on my page (tabstrip-multipage).

Your method is only working when I click a button on the first tab.
If I click a button on one of the other tabs, I get an error: 'options' is empty or not an object.


Wie vroeg sterft, is langer dood
0
Joop_
6/2/2005 7:38:57 AM
then use

document.getElementById('DropDownList1').value 

that is if that tab thing even has a  value....


Eric
Pascarello.com | Twitter epascarello | LinkedIn
0
A1ien51
6/2/2005 1:25:49 PM
Thanks for your help. It's all working now! Yes [Y]
Wie vroeg sterft, is langer dood
0
Joop_
6/2/2005 1:39:15 PM
Reply:

Similar Artilces:

opening new window from code behind using javascript
Hello,I am trying to open new window when i click on a button using javascript Page.ClientScript.RegisterStartupScript(this.GetType(), "openwindow", "window.open('WebForm2.aspx','','height=200,width=300');", true);this is the code i am using.But the problem is it postsback whole page.Can some body tell me how i can open new window without posting back the page.ThanksVinod   Hi Vinod,Do you use a scriptmanager on your page?If so, then you should use the RegisterStartupScript method of the ScriptManager class (= ScriptManager.RegisterStar...

I am using a client side javascript to open a new window and have a problem
I have a user control..On it i have the word 'open' as a link....calling my javascript to open another page.....This page then auto close with javascript...Problem is when you click on 'open' it brings up my page and another page about:blank.......how do you avoid this???? hello, u would like to check my article on the following address: Pop Up Window from inside ASP.NET best of luck.Bilal Hadiar, MCP, MCTS, MCPD, MCTMicrosoft MVP - Telerik MVP...

What would the JavaScript be to open a server file in a new window server side?
I have a button on a form and I want to register some JavaScript that will open a server file in a new window.  I am thinking along the lines of: window.open("\\myservername\subdirectory\myfilename.doc");  I know the above code is not correct, and I was wondering how to do this?  Is it posible with a path like that of above? Thanks!Thank you, Did you try that and what happened? EricPascarello.com | Twitter epascarello | LinkedIn It does not work.Thank you,  so why not using the full path? ex: http://...or a relative path to the file: ex: window.open(&...

how to call javascript function written within js file from server side code (code behind file) ?
Hi, I have js file containing all javascript functions required for my page. I am using ScriptManager.RegisterStartupScript to call javascript from code behind file. But for this i must  specify all javascript code from code behind file only. For eg.  if i use Page.ClientScript.RegisterStartupScript(typeof(System.String), this.ClientID + "_Init", strScript); where strScript contains <script>alert("hi");</script>, on page_Init event, this script is called. But when same script is embeded within a function  and i try to give function name in ...

how to open popup window through javascript in code behind( popup window should be aligned center)
I want to use javascript in code behind to open popup window. I have Datalist inside which there is linkbutton. On click of linkbutton open popup window through javascript & that pop up window should be aligned centraly.Regards,Mahesh--------------------click "Mark as Answer" on the post that helps you. Friend, Try this sample . <html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">    <title>Untitled Page</title>        <style>    .blue{ bac...

Open new window from code behind
Right now, I have a new window being opened from a button event and javascript on the client side.... Button event... Page.RegisterStartupScript("startTest", "<script language='javascript'>getTest()</script>"); Client side javascript....  <script language="JavaScript">  function getTest(){ openTest=open("t1.aspx","","height=700,width=850,resizable=yes");}  </script> The thing is, I now do NOT want the javascript to be seen on the client side, but on the code behind page.  The user should not be able to know the url ...

Opening a new window from the code behind
Hi, On my web page I have a button which runs some code to update values. At the start of this code in the code behind I'd like to open another web page and another at the end of the updates but I can't work out how I do that. I've gota javascript window.open statement on my client side code, do I need to do something similar on the code behind ? Thanks for any help.  hi, I assume someone clicks on the button on page 1? Button1.Attributes.Add("onclick", "window.open('Page2.aspx','popWin','width=300px,height=200px');"...

"Open or save file dialog" not opening on IE registering a javascript block in my page's code behind
Hi,I have a problem with a piece of code that appears to work on some systems but fails on others.I have a button in my ASP.NET 2.0 page that downloads a file from a SQL database to a temporary folder in my web server. After that it registers a javascript block to open the file on the calling client. The aim is to present  the “open or save” dialog for the downloaded file (an excerpt of the function is at the bottom of this message). This perfectly works on my debug machine (XP SP3, IIS 5.1, IE7) and on several others but fails (does nothing) on some more machines. For example I have th...

Open page in a new window from Code-Behind
Is there a way to make a regular button open a page in a new window the same way you can with a hyperlink from a code behind file? I was building a JavaScript string in the CB and then doing a Response.Write but there were was an issue. I was forced to to write a period to the original page before the JavaScript Response.Write would fire, which would push my page down just a bit and I thought it was sloppy. In the interim I wrote something else but it takes the user away from the main page and that is not the optimum solution. hello, check my article here at my weblog customize it to be us...

javascript in c# code behind for window.open()
Hi all, So I have this code that I am using to pass variables in the query string but also use javascript in order to have a window.open and window.close function on both pages.string WindowOpen = "window.open('Notes.aspx?NoteTableId=" + id + "&NoteTable=" + Tables.InvoiceHeader + "'); return false;"; linkNoteButton.Attributes.Add("onclick", WindowOpen);   My question is where in that string would I implement things such as Heigth and Width of the window being open, also like setting menubar and toolbar = 0 so that those aren'...

open .aspx into new window from code-behind
hi, is there a way to direct a .aspx page to open into a new window from code executed within code-behind ??? Server.Transfer and Response.Redirect do not do the trick... perhaps Server.Execute ? any help would be much appreciated, thanx. you can use RegisterClientScriptBlock to add a short chunk of client-side javascript to open the pop-up when the main window refreshes. But you can't do what you describe directly....

open .aspx into new window from code-behind
hi, is there a way to direct a .aspx page to open into a new window from code executed within code-behind ??? Server.Transfer and Response.Redirect do not do the trick... perhaps Server.Execute ? any help would be much appreciated, thanx. ...

VB.NET Code to open a new window
Hello,I want a new page to open up, call downloadfile, the close the page again.  how do I do that? I have a timer1_Tick going on, once a file exists, it will open a new page, call the download, and close again. On the parent page open a new window using javascript  Call the below function on the link <a href = '#' onclick = "popUp()" >DownLoadFile</a>var Popup;function popUp() { Popup = window.open("DownLoad.aspx", "Popup", 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=420,height=300,le...

How do I open a new Browser Window from the code behind.
If I need to open a new browser window from an aspx I would run: <asp:HyperLink id="hyperlink1" ImageUrl="images/pict.jpg" NavigateUrl="http://www.microsoft.com" Text="Microsoft Official Site" Target="_blank" runat="server"/> Where target="_blank" render the content in a new window. How can I do it using code behind (vb) (i.e. from xxx.aspx.vb)? Tx Job You can't do it directly, but you c...

Web resources about - Open new window with javascript from code behind file - asp.net.client-side

JavaScript Kit- Your comprehensive JavaScript, DHTML, CSS, and Ajax stop
Your comprehensive JavaScript, DHTML, CSS, and Ajax stop

Let's Code: Test-Driven Javascript by James Shore — Kickstarter
James Shore is raising funds for Let's Code: Test-Driven Javascript on Kickstarter! A screencast series focusing on rigorous, professional Javascript ...

Eloquent JavaScript: A Modern Introduction to Programming
... and pragmatics. I loved the tutorial-style game-like program development. This book rekindled my earliest joys of programming. Plus, JavaScript! ...

Lightbox (JavaScript) - Wikipedia, the free encyclopedia
On a Lightbox-enabled page, a user can click an image to have it magnified in a Lightbox window, which resizes itself according to the size of ...

Open Directory - Computers: Programming: Languages: JavaScript: Tools: Obfuscators
about dmoz - help Top : Computers : Programming : Languages : JavaScript : Tools : Obfuscators Tools for obfuscation/compression JavaScript code. ...

Facebook File-Transfer App Pipe Relaunches, Now Works Via JavaScript, WebRTC
... allowed Facebook users to send and receive files up to 1 gigabit in size. A new version of Pipe debuted Monday, and the app now works via JavaScript ...

Under the Hood: The JavaScript SDK - The use of polyfills - Facebook
Facebook Engineering hat eine Notiz mit dem Titel Under the Hood: The JavaScript SDK - The use of polyfills geschrieben. Du kannst den vollständigen ...

JavaScript SDK - Facebook-Entwickler
Overview The JavaScript SDK provides a rich set of client-side functionality for accessing Facebook...

Senior Front-End Developer (Javascript and AJAX)
Company: Sometrics Category: Software Development Location: Los Angeles, CA How to Apply: jobs@sometrics.com Description: We are looking for ...

Badass JavaScript (@badass_js) on Twitter
WebGLWorker is an open source library that allows using WebGL from Web Workers. Great work as always from @kripken . blog.mozilla.org/research/2014/… ...

Resources last updated: 12/2/2015 11:45:57 AM