Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,

I am trying to open a popup window on click of a button.I have tried the following code but it is not working:

Button btn = Page.FindControl("btndefer") as Button;
btn.Attributes.Add("onClick","javascript:window.open('DeferReason.aspx','width=500,height=245,left=350,top=400')");


Have also tried:
btndefer.Attributes.Add("onClick", "window.open('DeferReason.aspx'); return false;");


Also tried
string popupScript = "<script language='javascript'>" +
     "window.open('DeferReason.aspx ',  " +
     "'width=200, height=200, menubar=no, scrollbars=yes')" +
     "</script>";
      Page.RegisterStartupScript("PopupScript", popupScript);


How to make it working?

Bye
Posted
Comments
thatraja 30-May-11 13:21pm    
Any error? check in firefox error console. If yes then include that in your question & let me know.
Also ensure if your popup blocker not blocked that popup window.

Try this

XML
<%@ Page Language="C#" %>
<%@ Import namespace="System.Text" %>
<script runat="server">
    void Page_load(object sender, EventArgs e)
    {
       Button1.Attributes.Add( "onclick", "popWin();return false;" ); 
     }
</script>
<html>
<head>
  <script>
   function popWin(){
        window.open('http://msdn.microsoft.com', '', '');");
   }
   </script>
    </head>
<body>
    <form runat="server">
        <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Go!"></asp:Button>
    </form>
</body>
</html>
 
Share this answer
 
If ur using button then u can use OnClientClick like

C++
<asp:Button ID="Button1" OnClientClick="return testing();" runat="server" Text="Button" />


in javascript write like

C#
function testing()
   {
    window.open("Default2.aspx");
    return false;
   }
 
Share this answer
 
The Error in your code is that
C#
Button btn = Page.FindControl("btndefer") as Button;
btn.Attributes.Add("onClick","javascript:window.open('DeferReason.aspx','width=500,height=245,left=350,top=400')");

when you write this code what .net does is this in html


<input type="submit"  önclick="javascript:window.open(" deferreason.aspx="," width="500,height=245,left=350,top=400')'" value="Error Code"></input>


So in the above you see that the before the onClick .net puts single Quotes and you have put single Quotes in the code as well so it the quotes is the problem

So try this

C#
Button btn = Page.FindControl("btndefer") as Button;
btn.Attributes.Add("onClick","javascript:window.open(\'DeferReason.aspx\',\'_blank\',\'width=500,height=245,left=350,top=400\')");


See this revised code
 
Share this answer
 
v2
Comments
Kaira_29 30-May-11 8:53am    
it's not working
Steven.Pinto2000 31-May-11 4:50am    
and also see the second parameter of window.open over here it is also wrong
http://www.w3schools.com/jsref/met_win_open.asp

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900