Thursday, July 03, 2008

ASP: Launch new window on button click

One way to launch a new window when a button is clicked is by associating a javascript with the onlick event. This is best done when the page is loaded:

protected void Page_Load(object sender, EventArgs e)
{
Button1.Attributes.Add("onclick", "javascript:window.open ('http://www.microsoft.com/'); return false;");
}


where Button1 is an 'asp:button'.
Also, note the 'return false;' part of the script: this makes sure that the page is not posted back to itself.

No comments: