1 / 21

What To Do and What Not to do in ASP

What To Do and What Not to do in ASP.net. By: Ryan Fishman. Vb.Net Basics 1. Same Logic type stuff such as If, Select Case, Do, While, For and thing like that. Should be very easy to find what the syntax is exactly on google When Doing Setters and Getters like you would in Java please use

benniek
Download Presentation

What To Do and What Not to do in ASP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. What To Do and What Not to do in ASP.net By: Ryan Fishman

  2. Vb.Net Basics 1 • Same Logic type stuff such as If, Select Case, Do, While, For and thing like that. Should be very easy to find what the syntax is exactly on google • When Doing Setters and Getters like you would in Java please use Private newPropertyValue As String Public Property NewProperty() As String Get Return newPropertyValue End Get Set(ByVal value As String) newPropertyValue = value End Set End Property

  3. Vb.Net Basics 2 • Generics also exists in VB.net like they do in Java. An example use of them are the following • Dim T as New List(of String) • Public Class GenericClass(of T) Private newPropertyValue as T Public Property NewProperty() As T Get Return newPropertyValue End Get Set(ByVal value As T) newPropertyValue = value End Set End Property End Class

  4. Vb.Net Basics 3 • For Each loop is useful for going through an Array. • An Example would be Dim Employees as New List(of Employee) For Each EE as Employee in Employees EE.PrintValue() Next

  5. SQLReading • To do anything in SQL make sure you import the following • Imports System.Data • Imports System.Data.SqlClient • To Read From SQL Copy and Paste this code and keep on reusing it. Private Sub LoadSchedule() Dim SelectString As String = "SELECT ScheduleID, GameNumber FROM Schedule" Dim MyConnection As SqlConnection Dim MyCommand As SqlCommand Dim MyReader As SqlDataReader MyConnection = New SqlConnection() MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings("Umpires").ConnectionString MyCommand = New SqlCommand() MyCommand.CommandText = SelectString MyCommand.CommandType = CommandType.Text MyCommand.Connection = MyConnection MyCommand.Connection.Open() MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection) While MyReader.Read() Dim ScheduleID As String = MyReader.GetValue(0) Dim GameNumber As String = MyReader.GetValue(1) Dim TempGame As New FlexiGame(ScheduleID, TempFlexiGameNumber,_ ListOfGames.Add(TempGame) End While MyCommand.Dispose() MyConnection.Dispose() End Sub

  6. SQLWriting • To Write to the data base do this • Dim SQLString as String = “UPDATE Table Set Column1 = ‘hello’ WHERE TableID = ‘1’” • DataSource.WriteDataBase(SQLString)

  7. InheritanceAbstract Class • Vb.net’s version of Abstract Classes is Public MustInherit Class SuperClass End Class • You can code this class like any other class but this class does not create objects just like in Java • You can have Properties, Functions or Subs that must be overrided in the the child class by doing • Public MustOverride Function MyFunction() as Object • You can also have Properties, Functions or Subs that may be overrided in the the child class by doing • Public Overridable Function MyFunction() as Object

  8. InheritanceChild Classes • A Child Class always Inherits from a superclass by doing • Public Class MyChildClass Inherits SuperClass End Class • Child Classes Override Super Classes Properties • An Example would be Public Overrides Function SendHello() As String Return “Hello” End Function

  9. Control Flow New Init LoadControlState Load Events PreRender SaveControlState

  10. New • Use this to load initial properties • An example would be if you were to make your own custom textbox you might to Public Sub New(Text as String) Me.Text = Text End Sub

  11. Init • You would put code here for things you need to do before the load or the loadcontrolstate, generally it is not a good idea to put code in here.

  12. LoadControlState Asp.net has no memory across postbacks. A solution to this problem is to put date into the ViewState (written right into the html) and store it in the actual page. When a postback occurs, the hidden variables inside the webpage are put into savedState object for easy retrieving. You may only put objects into the savedState that are serializable meaning strings, dates, numbers and booleans.

  13. Load • Here is where you should build your control. You should put your controls into tables and then do Me.Controls.Add(MyTable). You will not be able to retrieve the values of controls that were built by your control (example if you made a textbox). Controls should be built in the following Order • Delcare Your Controls (Dim MyTable as New Table) • Link Up your Control(Me.Controls.Add(MyTable) • Set the ID’s for your Control (MyTextBox.ID = “MyText” • Do any initial Set Up to the values of your control

  14. Events • Events will automatically be fired when the event that is required on the webpage for the event to occur occurs. • The entire Control state should be loaded by the time the event fires. • Controls can be rebuilt at this point if need be but control states won’t be automatically loaded as they were before with the OnLoad event. • Two types of ways of registering Events • Private WithEvents Button as Button • Dim Button as New Button AddHandlerButton.Click, AddressOfButton_Click ‘Somewhere in the OnLoad Event Public Sub Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim TempButton As Button = sender Dim TempIndex As String = TempButton.ID ‘use this to figure out which control was clicked incase you ‘have an array of buttons End Sub

  15. Pre Render • By the time the event occurs control state should be loaded so this event is useful for getting values for controls built by your control. • You are still permitted to rebuild controls at this point but Control States will not automatically load as it did with the OnLoad Event.

  16. SaveControlState • Use this method save important information that is required across postbacks. An example would be if you were making a gridview control, you may want to save which row is in Edit Mode.

  17. ID Types • ID • The ID of the control. You should always specify an ID for control that are involved in postbacks • (examples: textbox, button, linkbutton, validationcontrols) • (Non Examples: labels, table elements) • Make sure ID’s are unique or errors will occur and ASP.net will not tell you why. • ID’s should only include letters, numbers and underscores nothing else • Good Example of ID “Hello_World_TextBox” • Very Bad and will cause lots of unknown errors that Asp.net won’t be able to help you with example “Hello.World.TextBox” or “Hello,World,TextBox” • UniqueID • Useful for finding controls embedded in other controls. • ClientID • This will be the id written inside the HTML, useful when a custom made javascript needs a controls ID

  18. Validation Controls • Several Validation controls most important for this project being RequiredFieldValidator and RegularExpressionValidator • Validates Controls without Postback • Validation involves one or more textbox, one or more validators, one validation groupe and one button. • Each Item that is to be validated, the validator and the postback button should be given a ValidationGroup • Display for the validator should be set to Dynamic and not Static • Don’t forget to set the ControltoValidate property and the Error Message

  19. Page Properties • Every Control can get access to the Page properties by doing Me.Page • Page.IsPostBack returns true if the page is being posted back and returns false if the page is being loaded for the first time • Page.IsValid returns true if all the validation controls validated properly and false if they didn’t, it is important to check this because Validation Controls can be hacked to return garage values and break the site • Use the following to register startup JavaScript. If Not Page.ClientScript.IsClientScriptBlockRegistered("_nco_gdir") Then Page.ClientScript.RegisterStartupScript(Me.Page.[GetType](), "_nco_gdir", javaScriptString) End If

  20. AJAX • Allows for partial page postback which will decrease postback time and reduce page flicker. • Two ways of doing AJAX • Using complicating JavaScript • Using Update Panels • Two ways of doing AJAX • When adding controls to an UpdatePanel, you need to do UpdatePanel1.ContentTemplate.Controls.add(MyControl) as opposed to UpdatePanel1.Controls.Add(MyControl) • When you have nested UpdatePanels make sure to put UpdateMode=Conditional or else all UpdatePanels will update which is usually not what you want.

  21. Ajax Control Toolkit • Supplies some nice ready to use controls that implement AJAX. • Some of you will be using some of the controls from, the most important ones being the CalenderControl and the MaskedEditControl check out http://www.asp.net/AJAX/AjaxControlToolkit/Samples/Calendar/Calendar.aspx for more information. • Don’t forget to do Imports AjaxControlToolkit before using.

More Related