How to Create a New Window for a Web Browser Using VB.NET
One of the challenges of working with client-side programming languages is getting Web browsers to respond without talking to the Web server. ASP.NET, Microsoft's Web development platform uses VB.NET as one of its programming languages. VB.NET alone cannot even open a new window because the code does not interact with browsers. However, you can launch new Web browser windows by adding JavaScript to your VB.NET application.
Instructions
-
-
1
Open your ASP.NET website project in Microsoft Visual Studio. Visual Studio places you project files in the Solution Explorer window. Locate the website's startup form, and double that form to view it in the Design window.
-
2
Press the "Ctrl," "Alt" and "X" keys to open the Toolbox. Double-click the "Button" control in the Toolbox to place it on your form.
-
-
3
Double-click a blank area on the form to open the code window. This code window contains the "Page_Load" sub that runs when the page loads.
-
4
Paste the following code into that sub:
Dim scriptCode As String
Dim URL As String
URL = "http://www.whitehouse.gov"
scriptCode = "window.open(" + "'" + URL + "')"
Button1.Attributes.Add("onclick", scriptCode)
The last statement adds an "onclick" attribute to the button you created. This event executes the JavaScript code stored in the scriptCode variable. The URL variable holds the name of the Web page the new window will open.
-
5
Press "F5" to launch the project. A browser window opens and displays your Web page. Click the button you added to the form. The button click runs the JavaScript function. That function opens a new browser window and navigates to the site stored in the "URL" variable.
-
1
Tips & Warnings
After testing your application, replace the value of "URL" with anything you like. It's current value points to the White House website.
References
- Photo Credit Jupiterimages/Comstock/Getty Images