Monday, July 16, 2007

Events, Data Types, Procedures, Classes in ASP.NET

1. Events, Data Types, Procedures, Classes
2. Events
A. Simple concept 1.
1. ASP.Net programming = place server controls on a Web Form then code for their events. Big change from old ASP.
B. Simple concept 2.
1. Event handling takes place at the server…NOT the browser
2. Example…coding a command button click event triggers a postback so the server can deal with the event code.
a) If multiple events then they are stacked and processed on the server when a postback occurs. (Try: put Response.Write(“tag”) in three event procedures (textbox, checkbox, radio button) with only one having a postback)
3. Only a few controls automatically postback with the user.
a) Some controls require the AutoPostBack to be set for immediate response.
b) Use this with caution
C. Simple Concept 3
1. Round trips to server are expensive…keep it simple.
D. Simple Concept4
1. Each round trip triggers a Page_Load
2. If Not Page.IsPostBack then
a) Do this stuff once when the page loads the first time.
3. End If
3. How Events work
A. Double click on a control = open’s the editor and creates the default event stub
B. Unlike VB6 the event name is arbitrary….can be anything
1. What is important is the Handles clause btnClick.Click
a) Must match object name
2. Handles can support multi objects as long as they all share the same type of e parameter….
C. All Events have two parameters
1. sender is the object that generated the event
a) Use with multiple objects in the handles clause to determine which triggered this code
2. e holds all the event arguments…e.row, e.col, e.text etc etc….they are all buried in the e parameter in various properties
4. Common Button
A. Remember the command button will always trigger a postback
1. It creates an HTML submit button.
2. On the server the page Load event will fire
3. The button event will be handled
4. And the modified page will be returned
5. The ListBox
A. Made of item objects
1. listbox.Item.Add(“text”) adds an item
B. Selecting an Item triggers the SelectedIndexChanged event
1. Listbox.SelectedItem.Text = the item selected.
2. But does not trigger a postback
a) Must set the AutoPostBack=True
6. Radio Buttons
A. RadioButton…requires a “Group” name to group together
1. Can select only one from a group at a time.
B. RadioButtionList: Used with databinding to display a set of records as radio button list.
7. Radio Button raises CheckChanged event
A. Requires the autopostback like the ListBox
B. Work as a group set the groupname property
1. Group List is similar but does to require a groupname
C. But the CheckChanged event works independently on each button
1. You want the same code to run on both like a control array in VB
D. Modify the Handles clause on one CheckChanged event so it can handle the event for many objects.
1. …..handles rbtBlack.CheckedChanged, rbtWhite.CheckedChanged
2. Use “If sender” to determine which object triggered the code.
3. Note using the If sender Is operator (not =) because this is an object
E. Each PostBack = another Page_Load event
1. So: If Not Page.IsPostBack then….
a) Runs only code the first time
8. Data Types
A. Key points.
B. The Framework all share the same datatypes
1. Some of the old VB datatypes are different
a) Learn them as you go.
C. Option Explicit is turned on by default
1. Means you have to declare (Dim) a variable before you use it.
D. Option Strict is turned off but you SHOULD turn on before using it.
1. Means you have to explicit convert data types
2. Can NOT add strings (in VB you could)
3. To Turn on Option Strict : Project properties . Build
E. Can declare and initialize a variable at the same time
1. Dim intDemo as Integer = 100
9. Thoughts on Data Types
A. No Variants…(instead use object data type)
B. Currency is gone…use the decimal data type
C. Integers
1. Default is 32 bit: Most common form of non decimal type
a) Short is 16 bit
b) Long is 64 bit
D. Single & Doubles= Use with numbers with decimal point.
E. String = text
1. Char = 1 character
F. Date = Date and Time
G. Converting Strings
1. Use Object.ToString, CStr, CType
2. Note .ToString(“yyyy-MM-ddTHH:mm:ss”)
H. Use CType to convert between Data Types
1. Dim decDemo1 As Decimal = CType(200.23, Decimal)
2. CType(a,DataSet) …. Convert anything into anything
I. DirectCast(val,type)…faster but does not change data type… just declares it.
J. Or use CFuntions
1. Dim decDemo As Decimal = CDec(100.23)
10. Adding to self
For y = 1 To 10
x += y or
(x = x + y are the same thing.)
Read from right to left y is equal to x + self.
11. String.Format: Easy building and formatting of a string.
A. Used to create string blocks with embedded variables: Very cool
1. Instead of “string “ & data & “ string “ & data etc
B. strDemo=String.Format(“String block with {0} variables (1)”,”data in 0”,data in1)
C. Can format the values standard expressions such as with {0:C} = currency
12. Writing Procedures = reusable code: Easy
A. Concept…combine lines of codes under one name you can call when ever you need to run those lines.
B. Private Sub Name(parameters)
1. It will put in the End Sub
2. Parameters are passed as ByVal by default
C. Private Function name() as datatype
1. The value defined by RETURN is the result of the function
13. Creating Classes (encapsulating behavior)
A. Concept …. Combine (encapsulate) procedures under one name so you can create an instance of that class…and get all the procedures.
1. A class is a template or group of functions
B. Add a Class module to your project
C. Or create a class on a WebForm itself…
1. You create “additions” to the base class with your class
2. Go to the bottom on code and type Class name…end class will be inserted
D. Everything in .NET is based upon a base class
1. Check the first line of your web form
14. Creating properties
A. Public Property name as data type: Property User() As String
1. Get and sets will be inserted
2. Value (the set parameter) will be created
3. To create a readonly = Public ReadOnly Property
15. Public Function name() as data type = exposing a method
16. Instantiate your class (create an instance of the class)
A. Dim oClass as New ClassName … this is an Instance of ClassName
B. oClass.Method is the Instance.Method
1. Now you have access to all its properties and methods.
17. Using Public Shared: Do not have to Dim the object to use it.
A. Create the function as Public Shared
1. Public Shared Function ShareConnectString(…
B. To call it …. Class.Method name…this is called a Class Method
18. Shared Properties only work if the internal variable is also shared.
A. You will forget this and it will cost you a few hours of debugging.
19. Going to another page in code
A. Server.Transfer(“page”)
1. Works like a Goto in code.
2. Terminates execution of the current page and begins execution of a new page.
3. Does not require a round trip back to the browser
4. Note that the URL in the browser does not change.
B. Response.Redirect
1. Tells browser to Hyperlink to another URL
2. Can link to any location at any site.
3. No ‘html’ code prior to this statement
4. The URL is now the new page.