Basics of ASP.Net (Using Dreamweaver CS4)

yelpframeSecurity

Nov 4, 2013 (3 years and 9 months ago)

110 views

Basics of ASP.Net (Using Dreamweaver CS4)
Dreamweaver offers limited support for ASP.Net

To create an ASP.Net page in Dreamweaver, create a New Document: File > New…



Select “Blank Page” using “ASP.NET VB”





REMEMBER: TO VIEW YOUR ASP.Net PAGES YOU MUST CREATE A SITE USING SERVER SIDE TECHNOLOGY




In Code view you will see the following default layout. Dreamweaver does not provide support for
Codebehind



Adding Code Blocks
There are two
ways to insert your server-side script into an aspx
(1) Using Inline Code Blocks: <% %> and (aka: Code Render Blocks)
(2) And Script Tags (aka: Script Blocks) <script runat="server"> </script> (see p.6 below)

(1) Inline Code Blocks:
<% %>
• Inline Code Blocks can be placed anywhere on the page
• You can type then or use the menu: Insert > Tag


• Then select the Scriplet tag

• Then type in the object name you want to call: Response or Request or Server etc

• Dreamweaver Code complete will provide the properties and methods available
Examples:
<% Response.Write("This is Your URL: ") %>
<% Response.Write(Request.Url().ToString()) %>

Variables
In ASP.Net VB – a variable is defined using the Dim statement
(One of two origins: Dimension or “Declare in Memory”)
As VB.Net is a fully Object Oriented language you must declare the Variable type (As String,
Integer, Object, Boolean, etc…)

Syntax
In ASP.Net VB – Syntax is much less specific than in some languages (i.e., JavaScript)
Key words are case sensitive; there is no end-of-line mark (such as a semi-colon); there are no curly
braces to block code.

Loops / Conditional Statements
In ASP.Net VB – there are several loops, but the basic For / Next loop is shown below. The basid
If/Else statement is shown below


<%
Response.Write("<table><tr>")
Dim x As integer
Dim i As Integer
For i = 1 To 12
Response.Write("<td>" & i & "</td>")
If i Mod 4 = 0 Then
Response.Write("</tr><tr>")
End If
x = x + i
Next
Response.Write("</tr></table>")
%>


• You can also add ASP scripting to regular HTML with the <% %> tags
o Insert Tag and choose ASP tags, expression (notice the “=” assignment)

o Then, inside of an HTML tag add an ASP command (such as the Response.Write
command)
<input type="text" value="<% Response.Write(x) %>" />
o or use the expression shortcut (the “=” is the same as
Response.Write)

<input type="text" value="<%=x %>" />

The value of the variable “x” will be the value for the text box

• Other Response.Write techniques may include
• Date and Time
<%
Dim rightNow As DateTime = DateTime.Now
Response.Write(rightNow.ToString("MMM dd, yyyy"))
%>

• Random Number
<%
Response.Write("<br />")
Dim RandomClass As New Random()
Dim RandomNumber As Integer
RandomNumber = RandomClass.Next(1, 11) '
>= to 1 and < 11 (1 to 10)

Response.Write(RandomNumber)
%>



• Response.Redirect()

See Posting back to the same page


• The Request Object – used for retrieving information such as forms, and server variables
For example, if a page is posted, the Request object can retrieve the values from the posted page
a) Create a document with a form, set the action to an ASP.Net page (Example1.aspx), method: post
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="utf-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Posting Page</title>
</head>

<body>
<form action="Example1.aspx" method="post">
<input type="text" name="txtName1" />
<input type="text" name="txtName2" />
<input type="submit" />
</form>
</body>
</html>

b) On the target page add the following request objects

<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="utf-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Target ASP.Net Document</title>
</head>

<body>
<%
Dim firstName as String = request.Form("txtName1")
Dim lastName as String = request.Form("txtName2")
Response.Write("Your Name is: " & firstName & " " & lastName)
%>
</body>
</html>


(2) Script Tags
Scripted Code Blocks: <script runat="server"> </script> (aka: Code Declaration Blocks)
Placed above the HTML Head element; used to encapsulate Subroutines and Functions; requires:
runat=”server”
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
' Code goes here…
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>

</body>
</html>

Functions and Subroutines
• Functions and subroutines are the most basic building block of Active Server Pages – They are very
similar in that they both perform a distinct, logical task. Their syntax is nearly identical, and they
can both perform the same actions, except for ONE major difference: a function returns a value.

• A Function

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
Protected Function rNum(ByVal v1 As Integer, ByVal v2 As Integer) As Integer
Dim RandomObj As New Random()
Dim RandomNum As Integer
RandomNum = RandomObj.Next(v1, v2)

Return
RandomNum
End Function
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<input type="text" value="<%= rNum(2,9) %>" />
</body>
</html>



• A Sub

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Text1.Value = "hello"
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<input id="Text1" type="text" runat="server" />
</body>
</html>


• Parameters
• ByVal and ByRef (parameter variable definition)
When you pass a variable ByVal a new instance of the variable is created and given to the
routine being called. Any changes made to the value of this variable have no effect on the
value of the original variable that was passed in.

If you instead pass in a variable ByRef, any changes you make to this variable also change it's
value outside the routine (Global).

• Posting back to the same page

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If IsPostBack Then
Response.Redirect("http://www.microsoft.com")
End If
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" action="test3.aspx" runat="server">
<input id="Submit1" type="submit" value="submit" />
</form>
</body>
</html>


• Referring URL
<script runat="server">

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Not Request.UrlReferrer Is Nothing Then
Dim x As String = Request.UrlReferrer.ToString
Response.Write(x)
End If
End Sub

</script>



ASP.Net Web Server Controls
(aka the Standard Controls).
These are <asp:zzz… controls that enable you to display buttons, lists, images, boxes, hyperlinks,
labels, tables, (as well as more complicated controls) that work with static and dynamic data or
controls that act as containers for other controls. Although similar to HTML controls, they are
specific to ASP.Net and are run at the server.



To add an ASP.Net Web Server Control


Add an onclick event and in your script tag add a subroutine

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
Protected Sub Btn1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
If Btn1.Text = "Go!" Then
Btn1.Text = "Stop"
Else
Btn1.Text = "Go!"
End If
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" runat="server">
<asp:Button ID="Btn1" runat="server" Text="Go!" onclick="Btn1_Click" />
</form>
</body>
</html>






Other Important Objects


• Server Variables - a collection of server environmental variables

<table border="1">
<th colspan="2">ServerVariables</th>
<%
Dim svar as string
For Each svar in Request.ServerVariables
Call Response.Write("<tr>")
Call Response.Write("<td><b>" & svar & "</b>:</td>")
Call Response.Write("<td>" & Request.ServerVariables(svar) & "</td>")
Call Response.Write("</tr>")
Next
%>
</table>


or

Specific ServerVariable Name

<%=Request.ServerVariables("REMOTE_ADDR")%>





• Session Variables
Session Variables allow web developers to store visitor information by setting a server variable that
is accessible to the application for the duration of a web visitor's session. Session Variables are kept
on the Server, but they relate back to a client cookie, so the client browser must have cookies
enabled.

First test if the client has cookies disabled before using application variables

To test this example, disable cookies (i.e., Tools > Internet options: select the "Privacy" tab (Advanced)

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
' Make sure cookies are enabled on the client browser
Response.Cookies("testcookie").Value = "checkme"
If Request.Cookies("testcookie").Value <> "checkme" Then
'--- user does not have cookies enabled
Response.Redirect("index.aspx")
End If
End Sub


NOTE: Although Session Variables are a powerful tool in the web developer's arsenal;
Dreamweaver does not automatically generate all of the code necessary for them to work.


• Application Variables
Application variables are shared variables among all users of a web. Because Application variables
are global you need a global file to maintain them. ASP.Net uses the Global.asax file for this.


NOTE: Although Application Variables are a powerful tool in the web developer's arsenal;
Dreamweaver does not automatically generate all of the code necessary for them to work.






Example of a “count of active users” on a site using:
Session and Application Variables (and the Global.asax file)

Create a Global.asax file – This file should be at the root of your site folder
----
<%@ Application Language="VB" %>

<script runat="server">

Sub Application_OnStart()
' Set our user count to 0 when we start the server
Application("ActiveUsers") = 0
End Sub

Sub Session_OnStart()
' Change Session Timeout to 20 minutes (if you need to)
Session.Timeout = 1 ' reset to 20 – use 1 for testing
' Set a Session Start Time
' This is only important to assure we start a session
Session("Start") = Now
' Increase the active visitors count when we start the session
Application.Lock()
Application("ActiveUsers") = Application("ActiveUsers") + 1
Application.UnLock()
End Sub

Sub Session_OnEnd()
' Decrease the active visitors count when the session ends.
Application.Lock()
Application("ActiveUsers") = Application("ActiveUsers") - 1
Application.UnLock()
End Sub

</script>
----
Next, on any ASP.Net page (.aspx) you want to show active users, add the following:

Active Users: <%= Application("ActiveUsers") %>



Dreamweaver Vs. MS Visual Studio Express Edition

Adobe's Dreamweaver is more concentrated on web design than web development.
Visual Studio is more of a web development program and makes ASP.Net a breeze!

Download Visual Web Developer 2008 Express Edition (cost: $0.00)
http://www.microsoft.com/express/downloads/#2008-Visual-Web-Developer



NEXT
Accessing Remote Data
Databases and Flat files