Monday, August 17, 2009

Opening a new window from code behind on Button Click in ASP.NET

If you are looking out for server side code to open a new window on Button Click, then here's how to do so.

Add the following script to the section of your page

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


function openNewWin(url) {


var x = window.open(url, 'mynewwin', 'width=600,height=600,toolbar=1');


x.focus();


}


script>






Then add a Button Control in the following manner


<asp:Button ID="btnOpenPop" runat="server" Text="Open Pop"

onclick="btnOpenPop_Click" />





Finally add some code in the code behind file

C#


protected void btnOpenPop_Click(object sender, EventArgs e)

{

string url = "http://www.dotnetcurry.com";

ClientScript.RegisterStartupScript(this.GetType(), "OpenWin", "");

}



VB.NET


Protected Sub btnOpenPop_Click(ByVal sender As Object, ByVal e As EventArgs)

Dim url As String = "http://www.dotnetcurry.com"

ClientScript.RegisterStartupScript(Me.GetType(), "OpenWin", "")

End Sub

No comments:

Post a Comment