Manage Learn to apply best practices and optimize your operations.

Create a simple Web form page

How to do it.


.NET Developer Tip
(Receive this column in your inbox,
click Edit your Profile to subscribe.)

Create a simple Web form page
Stephen Walther

In a former tip, we discussed the Web form as an object. Now we go a bit further into a discussion of Web forms with this tip, excerpted from InformIT, which offers a discussion of how to create a Web form page for your project. The tip also offers two examples, one in C# and one in VB, of code that will create the Web form page.


We'll start with a simple Web Form Page. We'll create a page that randomly displays a fortune.

You should add a Web Form Page to an existing ASP.NET project. We [do that as follows].

  • Select Project, Add Web Form.
  • In the Add New Item dialog box, enter the name Fortune.aspx for the Web Form in the Name text box.
  • Click Open.

Now that you've created a Web Form Page, you can add server controls to the page from the Web Forms toolbox. We'll add a Label control to the page by performing the follow steps:

  • If the Toolbox window is not already open, select View, Toolbox.
  • Drag a Label control onto the Designer surface from under the Web Forms tab in the Toolbox.

The next step is to add some application logic to the page. We want to retrieve a random fortune from an ArrayList of fortunes and display the fortune in the Label control. To do this, perform the following steps: Double-click the Designer surface to switch to the Code Editor.
Enter the following code for the Page_Load() method:

C#
 
private void Page_Load(object sender, System.EventArgs e)
{
 // Put user code to initialize the page here
 ArrayList colFortunes = new ArrayList();
 Random objRan = new Random();

 colFortunes.Add("Good things will happen!");
 colFortunes.Add("Future looks bright!");
 colFortunes.Add("Stay in bed!");

 Label1.Text = colFortunes[ objRan.Next( 3 ) ].ToString();
}
VB.NET
Private Sub Page_Load(ByVal sender As System.Object, 
ByValue As System.EventArgs) Handles MyBase.Load
 'Put user code to initialize the page here
 Dim colFortunes As New ArrayList()
 Dim objRan As New Random()

 colFortunes.Add("Good things will happen!")
 colFortunes.Add("Future looks bright!")
 colFortunes.Add("Stay in bed!")

 Label1.Text = colFortunes(objRan.Next(3))
End Sub

Finally, we are ready to compile and view our Web Forms page. Right-click the Web Form in the Solution Explorer window and select Build and Browse. The Web Form Page will be opened in a browser and you should see a fortune. Whenever you click the Refresh button, a new fortune is randomly selected and displayed.

TIP
If you receive an error when building your code, select Build Solution from the Build menu. A list of errors in your code will pop up in the Task List window. You can double-click an error to go directly to the error in your code.


To read the article from which this tip comes, click over to InformIT. You have to register there, but the registration is free.

For More Information:

  • Looking for free research? Browse our comprehensive White Papers section by topic, author or keyword.
  • Are you tired of technospeak? The Web Services Advisor column uses plain talk and avoids the hype.
  • For insightful opinion and commentary from today's industry leaders, read our Guest Commentary columns.
  • Hey Codeheads! Start benefiting from these time-saving XML Developer Tips and .NET Developer Tips.

  • Visit our huge Best Web Links for Web Services collection for the freshest editor-selected resources.
  • Visit Ask the Experts for answers to your Web services, SOAP, WSDL, XML, .NET, Java and EAI questions.
  • Choking on the alphabet soup of industry acronyms? Visit our helpful Glossary for the latest industry lingo.
  • Couldn't attend one of our Webcasts? Don't miss out. Visit our archive to watch at your own convenience.
  • Discuss this article, voice your opinion or talk with your peers in the SearchWebServices Discussion Forums.

This was last published in April 2003

Dig Deeper on Microservices pattern, platforms and frameworks

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchSoftwareQuality

SearchCloudApplications

SearchAWS

TheServerSide.com

SearchWinDevelopment

DevOpsAgenda

Close