ASP/ASP.NET: Tricks and Tips
How to get Microsoft’s Programming
Language to work for you
By Wade Tripp
Park University
[email protected]
Disclaimer
I am not an expert in all the details, just
giving a general overview of how it
works, and there are over a dozen ways
how to do things, with half of them
working.
Who are YOU?
History of ASP (abbreviated)
• VBScript
– Client Side
– Possible for all sorts of problems,
intentional or not
– Required the client to have IE installed
History of ASP (abbreviated)
• ASP
– Server Side Scripting
– Browser independent
– Required special server software
• but…..
– Very much interpreted and scripting
– Created many bad programming
habits
History of ASP (abbreviated)
• ASP.NET
– Server Side Scripting
– Able to program in many languages
– Permits good programming practice
• but….
– Steep learning curve
How ASP works
• Browser requests a web page
• Web server gets request and loads file
from hard drive
• Web server sends the file to asp.dll to
process
• Returns to the browser the processed
page
How ASP.NET works (interpreted)
• Browser requests a web page
• Web server gets request and loads the
file from hard drive
• Web server sends the file to
aspnet_isapi.dll to process
• Returns to the browser the processed
page
How ASP.NET works (compiled)
• Browser requests a web page
• Web server gets request and loads dll
from hard drive
• Web server sends the file to
aspnet_isapi.dll to process
• Returns to the browser the processed
page
• The code does not have to be on the
server
Differences between ASP
ASP
Quick and easy to use
and do
Easy to modify and
tweak
Uses the very basics
and forgiving
ASP.NET
ASP.NET
(Interpreted)
(Compiled)
Increased flexibility
No need to code
everything from
scratch
Memory of events
Still can get confusing
for large projects
Most flexible
Hardest to understand
Easy to get good
programming
standards
Differences between ASP
Simple and quick – ASP
Ex – Date modified, Alert Message
Repeated on several pages – ASP.NET Interpreted
Ex – Announcement on several web pages
Advanced Application – ASP.NET Compiled
Ex – Registration form
Getting down to code
ASP/ASP.NET handles the appearance,
input/output of a web page
The Code Behind focuses on the thinking and
language
“Code Behind” can happen in a web page itself
and does not have to be in a .vb or .cs file
Any language can use in theory, VB and C# are
the ones Microsoft has preconnected in
Visual Studio, but almost any can be used.
Java is not used for various legal reasons.
Simple Displaying Stuff
To simply display stuff ( traditionally used in
ASP ) you use <% %>
<% Response.Write ("Hello World") %>
<% = 2 + 2 %>
ASP is forgiving, so if you miss the ( ) in
Response.Write, it has variant type and other
items. But if you save it as .aspx it may not
work.
Is .asp the same as .aspx
No, but they are so close that most of the time
you can just do minor changes and tweaks
going from asp to aspx . Several things are
required (Declaration of variables)
The big change with CDONTS. The method for
sending out mail was changed from ASP to
ASP.NET IIS 5 to IIS 6 for various reasons
[Security being the main one]
QueryString vs. Forms
There are two ways to get information.
• QueryString/Get
– Information is sent via the URL
• Form/Post
– Information sent in the page
QueryString
<form method="GET">
It appears in the URL
.. /index.asp?Sample=test&B1=Submit
The ? Marks the end of the URL and the rest
are variables
Items are recoded for valid characters (make
spaces into +, & turned into %26 and other
items)
Advantage jump directly to section, performing
items
QueryString Disadvantages
Easily find the information via History and
guessable test.asp?ID=45
Limited on how much information can be
passed. Dependant on browser of 1000,
2000+ characters
Form
What is normally used with web pages
<form method="POST">
Better to make things go better and organized,
less chance of being reviewed
Actually using it
<%
Dim intNumber as integer
intNumber = Request.QueryString(“Test”)
intNumber = Request.Form(“Test”)
Response.Write (intNumber)
%>
TIP - Polish Notation
Based on ideas of Jan Lukasiewicz
Standard documenting is putting type in front of
name
• intName – Integer
• strName – String
• bolName – Boolean values
TIP - Polish Notation
Also good for ASP
• txtName – Textbox
• lblName – Label
• panName – Panel
• lstName – List
….
It keeps you organized
Basics of ASP.Net
<asp:TextBox id="TextBox1"
runat="server"></asp:TextBox>
When it is transformed by the compiler it
becomes
<input name="TextBox1" type="text"
id="TextBox1" />
What all happened?
ASP.NET is transformed into HTML
How does information get stored
Information is stored in a hidden view, called
the Viewstate
It sends and organizes the information for each
variable and items
<input type="hidden" name="__VIEWSTATE"
value="dDwxMKA0NDg0NTY3Ozs+LbpQGF
DDxjwkOEzOioQydd7xkTg=" />
The information is stored for all server objects
TIP – Don’t make everything label
Each ASP.NET item requires resources
Don’t use labels for standard items.
Why?
Because it takes extra resources and time to
download
TIP – Don’t make everything label
Each ASP.NET item requires resources
Don’t use labels for standard items.
Why?
Because it takes extra resources and time to
download
TRICKS – Storing information
Information can be handled
• Session
• Application
• Variable
• Value of ASP Object
• Hidden Field
• Passed Variable in Form
TRICKS – Database Reading
Can connect to database via connection with
SQL, Access, Informix, etc… anything that
permits ODBC
Warning – Access can cause problems with
older web servers
TRICKS – AutoPostback
Permits scripting to run after an item has been
altered
Requires Javascript
TRICKS – Validation
Lets a user check to make sure a field is
probably filled out
Normally uses Javascript to validate
There are a few ways around it (disable
Javascript)
TRICKS – Smart Navigation
Permits forms not to blink by using ‘IFRAME’
TRICKS – ME - ASP
Used to find what variables and objects are
used
Remove the Me. after you go though the form
Descargar

ASP.net Tips and Tricks - Missouri State University