Server Side Event Handling with ASP.NET 4.0 and VB
To demonstrate how to use server side events, we will create a simple web form with some buttons on it. Then, we will add code to the server side event handlers of these buttons to demonstrate how they work. To begin, create a new ASP.NET Empty Web Site and:
- Right click the project in your solution explorer.
- Select add new item…
- Select a web form.
- Name it ‘Default.aspx’.
- Click add.
- Open Default.aspx up to design mode.
- Drag and drop a label onto the web form.
- Add a break line.
- Drag and drop a button onto the web form.
- Add a break line.
- Drag and drop a button onto the web form.
- Add a break line.
- Drag and drop a button onto the web form.
- Add a break line.
- Drag and drop a button onto the web form.
- Add a break line.
- Drag and drop a button onto the web form.
- Add a break line.
- Drag and drop a button onto the web form.
We stand behind Server Intellect and their support team. They offer dedicated servers , and they are now offering cloud hosting!
We should now have a form with a label and 6 buttons on it. First, we need to add the event handlers for each of these buttons. To begin, open Default.aspx up to design mode and:- Double click Button1.
- Change the name of the generated event method from ‘Button1_Click’ to ‘Buttonx_Click’.
- Set the OnClick property of Button2 to ‘Buttonx_Click’.
- Set the OnClick property of Button3 to ‘Buttonx_Click’.
- Double click Button4.
- Double click Button5.
Dim b As Button
b = CType(sender, Button)
Select Case b.ID
Case "Button1"
Label1.Text = "You clicked the first button"
Case "Button2"
Label1.Text = "You clicked the second button"
Case "Button3"
Label1.Text = "You clicked the third button"
End Select
Label1.Text = "You clicked the 4th button"
I just signed up at Server Intellect and couldn’t be more pleased with my fully scalable & redundant cloud hosting! Check it out and see for yourself.
Then, add the following code to the Button5_Click event method:Label1.Text = Label1.Text + "You clicked the 5th button"
AddHandler Button6.Click, AddressOf Button4_Click
AddHandler Button6.Click, AddressOf Button5_Click
We used over 10 web hosting companies before we found Server Intellect. Our new server with cloud hosting,was set up in less than 24 hours. We were able to confirm our order over the phone. They responded to our inquiries within an hour. Server Intellect’s customer support and assistance are the best we’ve ever experienced.
This will call the events for button 4 and 5 when button 6 is clicked.Testing
To test this out, load up the web site and click all of the buttons. Ensure that the correct text appears in the label. This demonstrates how easy you can use server side events on your controls.
Server Event Handling asp4 vb
