April 26th, 2006

Open a New Browser Window From an ASP.Net Server Control

All ASP.Net controls add functionality to your web application by performing postbacks to the server. When they post back, their events are captured by the web server. So if you add an onclick event to a button, it will be a server-side event. But what if you need a button to open a window or JavaScript prompt when you click on it? This is a client-side event, and there's no way to add this property to an ASP.Net server control it would seem. Here's the code to allow you to do it programmatically.

Basically, we have to alter the HTML output of the button before the page loads or renders. The easiest way to do this is to add an attribute to the web control. So so we had a button control that looked like this:

<form runat="server" id="form1">
   <asp:button id="button1" runat="server" onClick="button1_click" />
</form>

The "button1_click" event refers to a method that is run on the server. Here's how to add the JavaScript to create a popup, popunder, alert, or whatever else you need to do on the client side:

void page_load(object s, EventArgs e)
{
    if (!IsPostBack)
    {
       button1.Attributes.Add("onclick", "window.open('help.htm', ", ", 'height=200,width=400'");
       // or alert user
       //button1.Attributes.Add("onclick", "alert('You clicked the button')");
       // or whatever
       //button1.Attributes.Add("onclick", "anyJavaScriptFunction");
    }
}

5 Responses to ' Open a New Browser Window From an ASP.Net Server Control '

Subscribe to comments with RSS or TrackBack to ' Open a New Browser Window From an ASP.Net Server Control '.

  1. Subodh said,

    on May 11th, 2006 at 5:54 am

    i want to open window on client machine on button click of main page
    let the client select some data from that window
    set the selected data to controls on main page
    and then i want execute some server code

    server code must be excuted after whole client code is executed

  2. Justin Cook said,

    on May 11th, 2006 at 11:10 am

    I don't really understand what you're asking

  3. Hovhannes said,

    on January 27th, 2007 at 4:43 am

    hello

  4. mark said,

    on May 11th, 2007 at 7:01 pm

    why not just do this in a button_click()?

    HyperLink1.Target = "_blank";
    HyperLink1.ResolveClientUrl("http://www.cnn.com");

    it's a piece of cake!

  5. N.Zaza said,

    on July 25th, 2008 at 6:40 pm

    Thank you good fellow

    Not only giving a nice solution, but explaining the cause of the problems when trying to do it with pure asp.net.

    You made my day end happy! wish the same for you every day

Leave a reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word