Lewis Clark
Lewis Clark

Reputation: 121

How to get a button to open a web page?

I've looked everywhere, including the MSDN forums, but no one has even mentioned this or ways to do it. The basic idea is that once a Button is dragged from the toolkit, how do you then link that button to a web page, ie I have a 'Facebook' button, how do I then make it so that when the button is clicked, Facebook opens in a new browser window?

Upvotes: 12

Views: 61158

Answers (4)

sujit jha
sujit jha

Reputation: 71

You can use Process.Start with the desired URL to open the default browser and navigate to the page:

using system.Diagnostics;

private void button1_Click(object sender, EventArgs e)
{
    Process.Start("http://www.YouTube.com");
}

Upvotes: 7

Santosh Panda
Santosh Panda

Reputation: 7341

You have to use Process class under System.dll which is be default added to the solution. But you should refer the namespace at the top of your class this way:

using System;

namespace MyApplication
{
    public class MyProgram
    {
        private void button1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.facebook.com");
        }
    }
}

Upvotes: 0

Jim O'Neil
Jim O'Neil

Reputation: 23764

Since you said open in a new browser window, I was thinking the context was an web application you were developing, so this would apply in that case:

Add an HTML button with the window.open JavaScript method. You don't need or want code-behind here because it's all happening on the client. Here's a snippet, and there are a few other options you can pass to window.open to control behavior.

        <input id="Button2" type="button" value="Facebook" onclick="window.open('http://facebook.com')"/></p>

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564373

Once you've dragged the button onto the designer, you can double-click on it to open up the Button's Click event handler. This is the code that will get run when the user clicks. You can then add the required logic, ie:

private void button1_Click(object sender, EventArgs e)
{
    // Launch browser to facebook...
    System.Diagnostics.Process.Start("http://www.facebook.com");
}

Upvotes: 20

Related Questions