Open new window from dropdownlist control

Last post 01-04-2007 7:20 AM by NC01. 18 replies.

Sort Posts:

  • Open new window from dropdownlist control

    10-25-2006, 11:57 PM
    • Loading...
    • shauns1
    • Joined on 10-19-2005, 2:09 PM
    • Posts 124

    Hi

     
    I have a dropdownlist that redirects to a new page on auto postback. The problem is that I would like this page to open in a new window. I have the javascript OpenWIndow function ready.

    What is the correct way to execute a JavaScript function from within .Net? I also need to pass the selected items value as a query string. Not a problem but I wasn't sure if this would make a difference to the answer?

     I have tried this, currently called by the dropdownlists onselecteditemchange event:

    Sub dlAllStockistsGO(s As Object, e As EventArgs)

        Dim intSID as Integer
        intSID = dlAllStockists.SelectedItem.Value
       
        If intSID > 0 Then
            'Response.Redirect("editStockist.aspx?SID=" & intSID)
       
            Dim openWindowScript As String = "<script language='javascript'>" & _
            "window.open('editStockist.aspx?SID=', 'edit', " & _
            "'width=850, height=500, menubar=no, resizable=yes, scrollbars=yes')" & _
            "</script>"

            Page.RegisterStartupScript("PopupScript", openWindowScript )

       
    End If

    End Sub 

     

    but it doesn't work. It throws an error saying that the sub that follwos this one is not declared??? 


    Many thanks

    Shaun
     

  • Re: Open new window from dropdownlist control

    10-26-2006, 12:22 AM
    • Loading...
    • shauns1
    • Joined on 10-19-2005, 2:09 PM
    • Posts 124

    The following doesn't work either: 

    dlAllStockists.Attributes.Add("OnSelectedIndexChanged", "javascript: var url=editstockist.aspx?SID=" & dlAllStockists.ClientID & ".options.value ; var win=window.open(url,null,'width=800, height=500, resizable,status=yes, toolbar=yes, menubar=yes, location=yes','true');win.focus()")

    I got this from http://forums.asp.net/thread/1067119.aspx 

    In the PageLoad event. No errors but nothing happens. 

  • Re: Open new window from dropdownlist control

    10-26-2006, 12:30 AM

    hi Shaun,

    could you try following

    step 1> add attribute to ddlist

    ddlist.Attributes.Add("onChange","javascript:openWin()"); //openWin is some javascript to open popup

    step 2. in this openWin check if

    document.all.ddlist.selectedIndex is not null or not = 0 //not 100% sure all i want to say is check if a valid item is selected

    then write your code window.open('page.aspx?id='+ddl.selectedIndex/value);

    hope you got what i wanted to tell.

     

    regards,

    satish.

    Kind Attn: If a reply to your post helped you, kindly mark it as Answered.
    __________________________________________________
    Please save Animals Help World Society For Protection Of Animals,
    Protect these speechless creatures of GOD
  • Re: Open new window from dropdownlist control

    10-26-2006, 12:41 AM
    • Loading...
    • shauns1
    • Joined on 10-19-2005, 2:09 PM
    • Posts 124

    I think thats where I'm struggling - I'm not too hot at JavaScript so its causing a problem.

     I understand what you want me to do but unfortunately do not know how to do it?!

    Could you possibly write down ALL the actual code that you think might work and I'll try it?

    Thanks again!
     

  • Re: Open new window from dropdownlist control

    10-26-2006, 12:47 AM

    hi,

    first of all this code might not be 100% perfect so please fight a bit on your own. 

    step 1> add attribute to ddlist in page_load

    ddlist.Attributes.Add("onChange","javascript:openWin()"); //openWin is some javascript to open popup

    step 2. in this openWin check if

    javascript

    function openWin()

    {

    if (document.all.ddlist.selectedIndex > 0)

    {

    var li = document.all.ddl.options[document.all.ddl.selectedIndex];

    var vv = window.open('page.aspx?id='+li.value);

    }

    }

    put this javascript function in html source i.e. .aspx file in <head></head> section

    <html> <head><script language = "javascript"></script></head><body></body></html>

    i hope it gives you a good start, do let me know if u still struggle.

    regards,

    satish

     

    Kind Attn: If a reply to your post helped you, kindly mark it as Answered.
    __________________________________________________
    Please save Animals Help World Society For Protection Of Animals,
    Protect these speechless creatures of GOD
  • Re: Open new window from dropdownlist control

    10-26-2006, 1:22 AM
    • Loading...
    • shauns1
    • Joined on 10-19-2005, 2:09 PM
    • Posts 124

    OK, this works but I have a further question...


    Step 1: Add Attribute to the dropdownlist control

     

    dlAllStockists.Attributes.Add("onChange","javascript:openWindowFromDropDownList('editStockist.aspx?SID=','edit','scrollbars=yes,resizable=yes,width=850,height=500')")
     

    Step 2: Write the javascript function and place within HTML Head tags as normal

     

    <script type="text/JavaScript">
    <!--

    function openWindowFromDropDownList(theURL,winName,features)

    {

    var SID = document.forms[0].dlAllStockists.value
    if (SID > 0){
    theURL = theURL + document.forms[0].dlAllStockists.value
      window.open(theURL,winName,features);
      }
    }
    //-->
    </script>

    Here I reference the ASP.Net control using its ID to get its value once rendered. I then check if anything has been selected and if so, open the new window.

    It works. So thats good.

    But...in my newly opened browser window I have a function to refresh the page that opened it. Opening the window with the above method doesn't seem to let me use the page refresh function in the opened window. However, if I open the new window with a simple hyperlink OnClick, I am able to use the refresh parent page function in the newly opened browser window.

    The refresh page function is:  onClick="window.opener.location.reload();

    It does nothing. As if the window doesn't think it has an 'Opener'.

    Any ideas as to why this might happen? 

  • Re: Open new window from dropdownlist control

    10-26-2006, 2:34 AM

    hi shaun

    where have u written onClick="window.opener.location.reload();" i cant see it

    can you try following please

    dlAllStockists.Attributes.Add("onChange","javascript:openWindowFromDropDownList('editStockist.aspx?SID=','edit','scrollbars=yes,resizable=yes,width=850,height=500');"self.reload();" ");

    i think it should work.

    check & let me know.

    regards,

    satish

    Kind Attn: If a reply to your post helped you, kindly mark it as Answered.
    __________________________________________________
    Please save Animals Help World Society For Protection Of Animals,
    Protect these speechless creatures of GOD
  • Re: Open new window from dropdownlist control

    10-26-2006, 11:02 AM
    Answer
    • Loading...
    • PeterBrunone
    • Joined on 06-19-2002, 9:15 AM
    • I'm standing behind you.
    • Posts 3,671
    • TrustedFriends-MVPs

    It should recognize its opener, but just in case, try this:

     

    <script type="text/JavaScript">
    <!--

    function openWindowFromDropDownList(theURL,winName,features)

    {

    var SID = document.forms[0].dlAllStockists.value
    if (SID > 0){
    theURL = theURL + document.forms[0].dlAllStockists.value
        var newWin = window.open(theURL,winName,features);
        newWin.opener = self;
      }
    }
    //-->
    </script>

     

    Peter Brunone
    MS MVP, ASP.NET
    Founder, EasyListBox.com
    Do the impossible, and go home early.
  • Re: Open new window from dropdownlist control

    01-02-2007, 7:05 AM

    hi,

    i tried aove mentioned solution.. but its not working..

    could u plz help me..

  • Re: Open new window from dropdownlist control

    01-02-2007, 7:33 AM

    hi satish,

    i tried in my web page..

    protected

    void dropCompany_SelectedIndexChanged(object sender, EventArgs e)

    {

    //dropCompany.Attributes.Add("onChange", "javascript:openWin()");

     

    dropCompany.Attributes.Add(

    "onChange","javascript:openWindowFromDropDownList('finsummary.aspx?SID=','scrollbars=yes,resizable=yes,width=850,height=500')");

    }

     and in html

    <

    head runat="server">

    <title>Untitled Page</title>

    <script language = "javascript" type ="text/javascript"> >

    function function openWindowFromDropDownList(theURL,features)

    {

    //window.open('http://www.yahoo.co.in');

    var

    SID = document.forms[0].dropCompany.value

    if

    (SID > 0){

    theURL = theURL + document.forms[0].dropCompany.value

    window.open(theURL,features);

    }

    </script>

    </

    head>

    its not working, any mistake let me know  

    regard,

    Jyoti

     

     

     

     

  • Re: Open new window from dropdownlist control

    01-02-2007, 8:01 AM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 9,503
    • TrustedFriends-MVPs

    Try this: 

    ///////////////////////////////////////////////////
    // In the parent window:
    ///////////////////////////////////////////////////

    Add this JavaScript:
    <script language=JavaScript>
    <!--
    function openWindowFromDropDownList(elementRef)
    {
     var selectedValue = '';

     for (var i=0; i<elementRef.options.length; i++)
     {
      if ( elementRef.options[i].selected )
      { 
       selectedValue = elementRef.options[i].value;
       break;
      }
     }

     if ( selectedValue.length > 0 )
     {
      var windowUrl = 'editStockist.aspx?SID=' + selectedValue;
      var uniqueValue = new Date();
      var windowName = 'Edit_' + uniqueValue.getTime();
      var windowFeatures = 'scrollbars=yes,resizable=yes,width=850,height=500';

      var windowRef = window.open(windowUrl, windowName, windowFeatures);
      windowRef.opener = self;
     }
    }
    //-->
    </script>

    In the CodeBehind, Page_Load event handler:
     GetPostBackEventReference(this, string.Empty);
         dlAllStockists.Attributes.Add("onchange", "JavaScript: openWindowFromDropDownList(this);");

    ///////////////////////////////////////////////////
    // In the child window:
    ///////////////////////////////////////////////////

    Add this JavaScript:
    <script language=JavaScript>
    <!--
    function closeWindow()
    {
     window.opener.__doPostBack('PostBackFromEditWindow', '');
     window.close();
    }
    //-->
    </script>

    In the CodeBehind, Page_Load event handler:
         closeButton.Attributes.Add("onclick", "JavaScript: closeWindow(); return false;");

    NC...

  • Re: Open new window from dropdownlist control

    01-02-2007, 10:57 AM
    • Loading...
    • PeterBrunone
    • Joined on 06-19-2002, 9:15 AM
    • I'm standing behind you.
    • Posts 3,671
    • TrustedFriends-MVPs

    It would also be good to know what "I tried it and it's not working" really means.

    We need to know exactly what you tried and what is happening now. 

    Peter Brunone
    MS MVP, ASP.NET
    Founder, EasyListBox.com
    Do the impossible, and go home early.
  • Re: Open new window from dropdownlist control

    01-02-2007, 11:44 AM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 9,503
    • TrustedFriends-MVPs

    Peter is right, since I tried both his and my code and both seem to work as expected. It might also be helpful to know exactly just what is expected to happen, since this is not always the same as what we expect to happen.

    NC...

  • Re: Open new window from dropdownlist control

    01-02-2007, 12:03 PM
    • Loading...
    • PeterBrunone
    • Joined on 06-19-2002, 9:15 AM
    • I'm standing behind you.
    • Posts 3,671
    • TrustedFriends-MVPs

    Dang, I knew I was going to regret not adding that.  Dinged for saving keystrokes again...  I need a "boilerplate support answers" entry on my clipboard.

     

    Peter Brunone
    MS MVP, ASP.NET
    Founder, EasyListBox.com
    Do the impossible, and go home early.
  • Re: Open new window from dropdownlist control

    01-02-2007, 12:29 PM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 9,503
    • TrustedFriends-MVPs