|
Buttons
A Button Web Server control is a control which we click and release to perform some
action. The class hierarchy for the Button control is as follows:
Object
Control
WebControl
Button
Button Event
The default event of the Button is the Click event which
looks like this in code:
Private Sub Button1_Click(ByVal sender As System.Object,_
ByVal e As System.EventArgs) Handles Button1.Click
'Implementation Omitted
End Sub
|
Notable Properties
CausesValidation: Gets/Sets the button that causes validation
CommandArgument: Gets/Sets the command argument which is
passed to the command event handler
CommandName: Gets/Sets the command name which is passed
to the command event handler
Text: Gets/Sets the caption on the button
Command Button
To create a command button you need to add a command name to the button. You add a
command name to the button with the CommandName property and a command argument with
the CommandArgument property. Both these properties can hold text which can be recovered
in code.
Sample Code1
Drag a Button onto the form from the toolbox and set it's text as ClickMe. When you
click the button it changes it's text from ClickMe to "You clicked Me". The code
for that looks like this:
Private Sub Button1_Click(ByVal sender As System.Object,_
ByVal e As System.EventArgs) Handles Button1.Click
Button1.Text = "You clicked me"
End Sub
|
Sample code to create a Command Button
Drag two TextBoxes and Buttons from the toolbox. Select Button one and in it's properties
window set the CommandName property to Command1 and CommandArgument property
to I am Command one. Repeat the same for Button two. When you click Button1, the CommandName
of Button1 will be displayed in TextBox1 and CommandArgument of Button2 in TextBox2.
To get those values we need to use the command event for the button and not the click
event. The code for that looks like this:
Private Sub Button1_Command(ByVal sender As System.Object,_
ByVal e As System.Web.UI.WebControls.CommandEventArgs) Handles Button1.Command
TextBox1.Text = e.CommandName
End Sub
Private Sub Button2_Command(ByVal sender As System.Object,_
ByVal e As System.Web.UI.WebControls.CommandEventArgs) Handles Button2.Command
TextBox2.Text = e.CommandArgument
End Sub
|
Live Code Demo
Sample 1
|