In this article I will explain how to open new popup window from
Server Side (Code Behind) in
ASP.Net using C# and VB.Net.
In order to open a new popup window from
Server Side (Code Behind), we need to use the
ClientScript RegisterStartupScript method to register the
JavaScript method to open new
window.
Note: You might also like to read: -
HTML Markup
The
HTML Markup consists of a Button control which will open a
Popup Window from
server side when clicked.
<asp:Button ID="btnNewWindow" Text="Open new Window" runat="server" OnClick="OpenWindow" />
Opening Popup Window from Server Side (Code Behind) using C# and VB.Net
Inside the
OpenWindow click event handler, the window.open
JavaScript function is passed is registered to execute on the
Page Load using the
ClientScript RegisterStartupScript function of
ASP.Net.
C#
protected void OpenWindow(object sender, EventArgs e)
{
string url = "Popup.aspx";
string s = "window.open('" + url + "', 'popup_window', 'width=300, height=100, left=100, top=100, resizable=yes');";
ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);
}
VB.Net
Protected Sub OpenWindow(sender As Object, e As EventArgs)
Dim url As String = "Popup.aspx"
Dim s As String = "window.open('" & url + "', 'popup_window', 'width=300, height=100, left=100, top=100, resizable=yes');"
ClientScript.RegisterStartupScript(Me.GetType(), "script", s, True)
End Sub
Note: It is very important to pass the 3rd parameter i.e. the options height, width, etc. when you want to open the page in New Window, if you skip that some newer browsers will open the page in New Tab instead of New Window.
Screenshot
Note: Newer browsers block Popup Windows that are opened automatically without clicking any link or button on the page and since here we open New Popup Window from server side some browsers might block it. Unfortunately, there is no solution for it and your site’s users must trust and disable popup blockers.
Demo
Downloads