Writing ASP.NET Pages

bubblemessengerSecurity

Nov 5, 2013 (4 years and 5 days ago)

118 views




1


Writing ASP.NET Pages



An ASP.NET page can be thought of as just like an HTML page that have
certain sections marked up for special considerations. An ASP.NET file is
created by saving it with the file extension ASPX. Web servers are
configured to looks f
or files with this extension and process by the
ASP.NET module (aspnet_isapi.dll) in the Web Server.


Unlike ASP pages which used to be written in VBScripts and/or
JavaScripts, an ASP.NET page is written with a full
-
blown programming
language. Several prog
ramming languages are supported to develop an
ASP.NET page, and VB.NET is the default language.


There are three ways of placing ASP.NET codes in a Web page:




Block scripting



Inline code blocks



Server Controls



Block Scripting Using <script> Tag


The f
irst method is to use a pair of <script> tags with the attribute runat =
‘server” attribute within the beginning <script> tag:


<script language = “VB” runat = “server”>

….Visual Basic.NET codes

</script>


The codes on right do not contain any
script but

the page is saved with the
ASPX extension


giving a static output as
an HTML page. View this page as an
HTML

or an
ASP.NET

page.



<html>


<head>


<title>Inserting ASP.NET code
Example</title>


</head>


<body>


Line1: First HTML Line<br />


Line2: Second HTML Line<br />


Line3: Third HTML Line<br />


</body>

</html>




2


<script language="VB" runat="server">

Sub Page_
Load()


Response.Write ("First ASP.NET Line<br />")


Response.Write ("Second ASP.NET Line<br />")


Response.Write ("Third ASP.NET Line<br />")

End Sub

</script>


<html>


<head>


<title>Inserting ASP.NET code Example</title>


</head>


<body>


Line1
: First HTML Line<br />


Line2: Second HTML Line<br />


Line3: Third HTML Line<br />


</body>

</html>

<html>


<head>


<title>Inserting ASP.NET code Example</title>


</head>


<body>


Line1: First HTML Line<br />


Line2: Second HTML Line<
br />


Line3: Third HTML Line<br />

<script language="VB" runat="server">


Sub Page_Load()


Response.Write ("First ASP.NET Line<br />")


Response.Write ("Second ASP.NET Line<br />")


Response.Write ("Third ASP.NET Line<br />")


End Sub

</scri
pt>


</body>

</html>

Next, the file is converted to include block scripts as follows and saved as
message2.aspx. Note the Page_Load () procedure that is used to write
some simple text on the Web page using the <script> tags.

View message2.aspx


[ Format of the Page: Script + HTML ]


<%@ Page language="VB" %>


<script runat="server">


' Insert page code here


'

</script>


<html>


<head>



</head>


<body>


<form runat="server">


<!
--

Insert content here
--
>


</form>


</body>

</html>



Note that the position of the scripts is at the top of the pages. However,
changing the position of the scripts within the pag
e does not change the
way the text is displayed on the browser as shown by the following
example, message3.aspx. However the output does not change from the
above file. This is because the scripts are in the Page_Load event which is
processed as soon as th
e page is loaded.

View message3.aspx




3


Inline Scripts Using <% %> Delimiters


The second method you can use to identify server scripts is
inline
scripting
that uses the
tags <%….%>. All codes within the inline script tags are
interpreted on the server. The benefit of this method is that codes can be
written anywhere in the page, intermingled with HTML codes, and without
calling any page events. Thus the output can be con
trolled.


<% response.write (“Hello World ! <br>”) %>


In all the above examples, the
Response object

on the server, which has a
method called
write
, is used to write texts on the browser.


Example below shows inline scripts intermingled with HTML codes
.















View message4.aspx


Note, the inline scripting in ASP.NET is no different from the ASP; however
in Block scripting there is some differences. For examp
le, for as ASP page,
the scripts in the previous page should refer to VBScript and there should
not be any Visual Basic subroutine as shown below.


<script language="VBScript" runat="server">


Response.Write ("First ASP.NET Line<br />")


Response.Write ("S
econd ASP.NET Line<br />")


Response.Write ("Third ASP.NET Line<br />")

</script>



<% Response.Write ("Hello! <br>") %>


<br>

<html>


<body>


Line1: First HTML Line<br />


<% Response.Write ("First ASP.NET Line<br />") %>


Line2: Second HTML Line<br />


<% Response.Write ("Second ASP.NET Line<br />") %
>


Line3: Third HTML Line<br />


<% Response.Write ("Third ASP.NET Line<br />") %>


</body>

</html>

<br>


<% Response.Write ("Goodbye!") %>




4


Server Controls


The inline scripts can create messy codes. Thus there is a server control
method that uses block scripts but places the output of the scripts in a
desired

position of a Web page. It separates the HTML codes from the
ASP.NET codes.


The following example shows a script code block executing at the
Page_Load event, however, the text is written by the ASP control:


<asp:label id=Message runat="server"/> <br />


















View Interweave2.aspx



Note that in this case, Response.Write is not used to display results on the
browser, rather an object named “message” is cr
eated in the Page_Load
event and it is called (by setting id = object name) within a special marker
called Server Control.



<script language="VB" runat="server">

Sub Page_Load()



Message.Text="The ASP.NET line"

End Sub

</script>


<html>

<head>

<title>Inserting ASP.NET code Example</TITLE>

</head>

<body>

First HTML Line<br/>

<asp:label id=Message runat="server"/> <br />

Second HTML Line<br/>

</body>

</html>




5


HTML Server Control Example:


The example above shows a simple ASP Server control using the text
“asp:” at the beginning of a con
trol. There are equivalent tags for HTML
Server controls. The following codes use a true dynamic action using an
HTML Server control: Input box.


<%@ Page Language="VB" %>

<script runat="server">



Sub Page_Load(sender as Object, e as EventArgs)


currentDT.Value = DateTime.Now


End Sub


</script>


<html>

<head>

</head>

<body>


<form runat="server">


<input id="currentDT" type="text" runat="server" />


</form>

</body>

</html>


View HTML Time Control


Again, note the separation of HTML and scripts within the Page_Load
event. The current time of the clock in the Web server is displayed through
an HTML Server Control: Input Box. The “id” pro
perty (currentDT) of the
Input control is used to reference the value as currentDT.value in the
script
.





ASP.NET Server Control Example:


The above codes can also be written using an asp control as shown below.


<%@ Page Language="VB" %>

<script ru
nat="server">



Sub Page_Load(sender as Object, e as EventArgs)


currentDT.Text = DateTime.Now


End Sub


</script>

<html>

<head>




6


</head>

<body>


<form runat="server">


<asp:TextBox id="currentDT" runat="server"></asp:TextBox>


<!
--

Insert content here
--
>


</form>

</body>

</html>


View ASP.NET Time Control


Note the HTML “Input” box is termed as “TextBox” in the ASP.NET

or Web
control. Both controls use “runat = server” property, meaning both are
executed in the server and only the HTML portion is retured to the browser.
Also note the difference in referencing the property of the HTML control
and ASP.NET control. The val
ue in the HTML “Input” box is referenced
through “currentDT.value” while that in the ASP.NET control is referenced
by “currentDT.text”. These are some differences that need to be carefully
looked at when programming HTML Server controls and ASP.NET Server
controls.


NOTE: It should be clear that HTML Server controls can be programmed and the
regular HTML controls that are traditionally used, can not be programmed. This
is the difference between regular HTML controls and HTML Server controls.


Also, what ca
n be done with HTML Server controls, can also be done with
ASP.NET/Web controls; however ASP.NET controls have some advantages. For
example, the output can be controlled by positioning an ASP.NET “label” control
in the form, which can not be done with an H
TML control.


Thus a dynamic web page can be created by a combination of HTML controls,
HTML Server controls, and ASP.NET Server Controls; however, traditional HTML
controls are used for static purposes and other two controls are used for
dynamic effect or

programming.


Event Handling
:


The previous example shows how ASP.NET built
-
in event handling takes place. Events
can be invoked by user controlled events such as through a mouse click. Below is an
example of user
-
defined event handling.


<%@ Page Lang
uage="VB" %>

<script runat="server">



' Insert page code here


'




7





Sub Button1_Click(sender As Object, e As EventArgs)


If age.Text < 21 then


results.Text = name.Text & ", you are a youngster!"


End If




If age.Text

>= 21 AND age.Text < 40 then


results.Text = name.Text & ", you are an adult."


End If




If age.Text >= 40 then


results.Text = name.Text & ", you are over the hill!"


End If


End Sub


</script>


<html>

<head>

</head>

<
body>


<form runat="server">


<p>


</p>


<p>


Name:


<asp:TextBox id="name" runat="server"></asp:TextBox>


</p>


<p>


Age:<asp:TextBox id="age" runat="server"></asp:TextBox>


</
p>


<p>


<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Click
Me"></asp:Button>


<!
--

Insert content here
--
>


</p>


<asp:Label id="results" runat="server"></asp:Label>


</form>

</body>

<
/html>




View the Result


Here an ASP.NET TextBox control is used to enter name, and another one is used to
enter age of a person. A Button is used to run a clic
k event (Button1_Click) and a label
(Results) is used to print the result depending on the age.


Note: Here “onClick” event for the Button is used to run “Button1_Click”
subroutine. This is an example of object
-
oriented and event
-
driving
programming. There

may be many subroutines on a form; however, only some
routines may be run depending on user actions.





8


Binding to a Database


ASP.NET provides a set of server controls to bind to a database that
eliminates many extra codes needed in ASP. The following c
ode example
shows how variables are declared, connection string is made to a open
database, values are retrieved from a table and are displayed on the
browser.

































View Datacontrol.aspx

<%@ Import Namespace="System.Data.OleDb" %>

<script runat="server">

sub Pa
ge_Load

dim dbconn,sql,dbcomm,dbread

dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" &
server.mappath("northwind.mdb"))

dbconn.Open()

sql="SELECT * FROM customers"

dbcomm=New OleDbCommand(sql,dbconn)

dbread=dbcomm.ExecuteReader()

customers.DataSource=dbread

customers.DataBind()

dbread.Close()

dbconn.Close()

end sub

</script>


<html><body>


<form runat="server">

<asp:Repeater id="customers" runat="server">


<HeaderTemplate>

<table border="1" width="100%">

<tr bgcolor="#b0c4de">

<th
>Companyname</th>

<th>Contactname</th>

<th>Address</th>

<th>City</th>

</tr>

</HeaderTemplate>


<ItemTemplate>

<tr bgcolor="#f0f0f0">

<td><%#Container.DataItem("companyname")%> </td>

<td><%#Container.DataItem("contactname")%> </td>

<td><%#Container.DataItem
("address")%> </td>

<td><%#Container.DataItem("city")%> </td>

</tr>

</ItemTemplate>


<FooterTemplate>

</table>

</FooterTemplate>


</asp:Repeater>

</form>


</body></html>




9


Binding to a Simple XML File


Like database binding, an ASP.NET data control can be used to connect to
an XML file. Then the results can be displayed on the browser in a table
format.


The following example f
irst shows the structure of an XML file,
countries.xml:




















View Countries.xml



<?
xml version="1.0" encoding="ISO
-
8859
-
1"
?>



<
countries
>

<
country
>



<
text
>
Norway
<
/
text
>




<
value
>
N
</
value
>




</
country
>

<
country
>



<
text
>
Sweden
</
text
>




<
value
>
S
</
value
>




</
country
>

<
country
>



<
text
>
France
</
text
>




<
value
>
F
</
value
>




</
country
>

<
country
>



<
text
>
Italy
</
text
>




<
value
>
I
</
value
>




</
country
>



</
countries
>






10


The following countries.aspx file uses the countries.xml file to display

the
attributes of the XML dataset.




























View Countries.aspx

and press a button.


<%@ Import Namespace="System.Data" %>


<script runat="server">

sub Page_Load

if Not Page.IsPostBack then


dim mycountries=New DataSet


mycountries.ReadXml(MapPath("countries.xml"))


rb.DataSource=mycountries


rb.DataValueField="value"


rb.DataTe
xtField="text"


rb.DataBind()

end if

end sub


sub displayMessage(s as Object,e As EventArgs)

lbl1.text="Your favorite country is: " & rb.SelectedItem.Text

end sub

</script>


<html>

<body>


<form runat="server">

<asp:RadioButtonList id="rb" runat="server"

AutoPostBack="True" onSelectedIndexChanged="displayMessage" /><p>

<asp:label id="lbl1" runat="server" /></p>

</form>


</body>

</html>