Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Getting Started with ASP.NET : Page 5

Learn what it takes to begin building ASP.NET Web sites with Visual Studio .NET. This article will provide you with the knowledge you need to jumpstart your foray into the world of ASP.NET development.


advertisement
Button and ImageButton Controls
When clicked, both Button and ImageButton controls submit the form that contains them to the Web server. What's the difference between these two types of button controls?

The Button control renders the same HTML as the <input type="Submit"> tag and displays on a page as a gray button. Table 8 lists Button control properties, methods, and events.

Table 8: Button control properties, methods, and events.

Property

Description



CommandName

Passes this value to the Command event when the button is clicked.

CommandArgument

Passes this value to the Command event when the button is clicked.

CausesValidation

Specifies if the button causes the form validation controls to validate the entries on the form.

Text

Specifies the text displayed on the button.

Methods

Description

OnClick

Raises the Click event.

OnCommand

Raises the Command Event.

Events

Description

Click

Raised when the button is clicked. The form containing the button is submitted to the Web server.

Command

Raised when the button is clicked. The values in the CommandName and CommandArgument properties are passed.


Clicking a button submits the form containing the button to the Web server and raises both the Click and Command events. The only difference between these events is that the Command event is passed two additional properties: CommandName and CommandArgument.

Interesting Fact #5: ASP.NET is very picky about closing tags, such as <form>...</form> or <asp:textbox>...</asp:textbox>. If you don't need to place anything between the tags you can save typing by closing the tag with />, for example, <asp:textbox />.
To handle the Click event of a button you could code a subroutine like the following:

Private Sub cmdSubmit_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles cmdSubmit.Click Me.cmdSubmit.Text = "Now you don't!" End Sub

The following code displays a text box, txtDisplay, and three buttons, cmdMultiple, cmdAdd, and cmdSubtract. Notice that each of the buttons has values specified for the CommandName and CommandArgument properties:

<asp:TextBox id="txtDisplay" runat="server" Text = 0 </asp:TextBox> <asp:Button id="cmdMultiply" runat="server" Text="Multiply" CommandName="Multiply" CommandArgument="1.5"> </asp:Button> <asp:Button id="cmdAdd" runat="server" Text="Add" CommandName="Add" CommandArgument="25"> </asp:Button> <asp:Button id="cmdSubtract" runat="server" Text="Subtract" CommandName="Subtract" CommandArgument="3"> </asp:Button>

The following code handles the Command event raised by clicking the cmdAdd, cmdSubtract, or cmdMultiply command buttons:

Private Sub PerformCalculation( _ ByVal sender As Object, _ ByVal e As _ System.Web.UI.WebControls.CommandEventArgs) _ Handles cmdAdd.Command, _ cmdSubtract.Command, _ cmdMultiply.Command Dim intNumber As Integer = _ e.CommandArgument If e.CommandName = "Add" Then Me.txtDisplay.Text += intNumber ElseIf e.CommandName = "Subtract" Then Me.txtDisplay.Text -= intNumber ElseIf e.CommandName = "Multiply" Then Me.txtDisplay.Text *= intNumber End If End Sub

Passing the "e" parameter in as the type CommandEventArgs makes the CommandName and CommandArgument properties for the clicked button available to the subroutine. The PerformCalculation example demonstrates how easy it is to build a single routine to handle events for related controls.

The ImageButton control is very similar to the Button control. The main difference between the controls is that you can use the ImageButton control to display an image instead of a plain button. Table 9 lists ImageButton control properties, methods, and events.

Table 9: ImageButton control properties, methods, and events.

Property

Description

AlternateText

Text displayed when the browser does not support images.

CommandName

Passes this value to the Command event when the button is clicked.

CommandArgument

Passes this value to the Command event when the button is clicked.

CausesValidation

Specifies if the button causes the form validation controls to validate the entries on the form.

ImageAlign

Specifies the positioning of the image. Options include AbsBottom, AbsMiddle, Baseline, Bottom, Left, Middle, NotSet, Right, TextTop, and Top.

ImageURL

Specifies the URL of the image to display for the button.

Methods

Description

OnClick

Raises the Click event.

OnCommand

Raises the Command Event.

Events

Description

Click

Raised when the button is clicked. The form containing the button is submitted to the Web server.

Command

Raised when the button is clicked. The values in the CommandName and CommandArgument properties are passed.


The following HTML code segment creates an ImageButton named ibtnCool that displays the button.gif image:

<asp:ImageButton id="ibtnCool" runat="server" ImageUrl="button.gif" AlternateText="Cool Blue Button" ToolTip="Click here for a surprise!"> </asp:ImageButton>

What are some uses for an ImageButton control? One thing you can use an ImageButton control for is to make a toolbar. You can use a single image for a toolbar containing all the button images you need. In the Click event you can check the value of the X coordinate of the control to determine which button was pressed.

Private Sub ibtnCool_Click( _ ByVal sender As Object, _ ByVal e As ImageClickEventArgs) _ Handles ibtnCool.Click Select Case e.X Case Is < 40 'Corresponds to the position 'of the Add button Case Is < 150 'Corresponds to the position 'of the Save button Case Is < 300 'Corresponds to the position 'of the Delete button End Select

In this article I'm just covering a few of the available Web controls. Additional Web controls you can use include Checkbox, CheckboxList, DataGrid, DataList, DropDownList, HyperLink, ListBox, Panel, PlaceHolder, RadioButton, and RadioButtonList.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date