Button, LinkButton and ImageButton controls in ASP.NET using VB.NET

This article shows simple Button, LinkButton, ImageButton controls in ASP. NET.
  • 5506

This article shows simple Button, LinkButton, ImageButton controls in ASP. NET.

1. Simple Button control:

The Button control is used to display a push button.

Properties: These are the following properties of button control.

bt1.gif
 

Figure 1.

Text - The text shown on the button.

CommandArgument - CommandArgument associated with the button.

CausesValidation - Gets/Sets the button that causes validation.

Button event:

Protected Sub cmdSquare_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdSquare.Click

End Sub

2. LinkButton

The LinkButton Web server control is a hyperlink style button control. It looks like a hyperlink control but is actually a button control with click and command events.

Properties: These are the following properties of LinkButton control.

bt2.gif
 

Figure 2.

Text - The text shown for the link.

CommandArgument - CommandArgument associated with the button.

CausesValidation - Gets/Sets the button that causes validation.

AccessKey - Keyboard shortcut for the control.

LinkButton event:

Protected Sub cmdSquareRoot_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdSquareRoot.Click

End Sub

3. ImageButton

The ImageButton control is used to display images and responds to mouse clicks on the image. The ImageButton control is used to display a clickable image.
 

Properties:  These are the following properties of ImageButton control.

bt3.gif
 

Figure 3.

OnClientClick - The name of the function to be executed when the image is clicked.

ImageUrl - The URL of the image to be shown.

ImageButton event:

Protected Sub cmdCubeRoot_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles cmdCubeRoot.Click                                                                                                                End Sub

For example:

Write a web form to create two label Number and Result. Now create two text boxes, one to input a number and other to output a number. Now create three different buttons as simple button, image button and link button to show Square, SquareRoot and CubeRoot of give number.

The form looks like this:

bt4.gif
 

Figure 4.

Now double click on the simple button and add the following code.

 

Protected Sub cmdSquare_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdSquare.Click

        Dim num As Double = Double.Parse(txtNumber.Text)

        Dim sq As Double = num * num

        txtResult.Text = sq.ToString()

    End Sub

Now double click on the LinkButton and add the following code.

 

Protected Sub cmdSquareRoot_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdSquareRoot.Click

        Dim num As Double = Double.Parse(txtNumber.Text)

        Dim sq As Double = Math.Pow(num, 1.0 / 2)

        txtResult.Text = sq.ToString()

    End Sub

Now double click on the ImageButton and add the following code.

 

Protected Sub cmdCubeRoot_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles cmdCubeRoot.Click

        Dim num As Double = Double.Parse(txtNumber.Text)

        Dim sq As Double = Math.Pow(num, 1.0 / 3)

        txtResult.Text = sq.ToString()

    End Sub

Now save and run the application.

bt5.gif
 

Figure 5.

Now when we press simple button(Square).

bt6.gif
 

Figure 6.

Now we press LinkButton(Square Root).

bt7.gif
 

Figure 7.

Now we press ImageButton(Cube Root).

bt 8.gif
 

Figure 8.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.