asp.net presented by pan gao. asp.net next generation of asp next generation of asp program language...
TRANSCRIPT
ASP.NETASP.NET Next generation of ASPNext generation of ASP Program Language to build web applicationProgram Language to build web application Part of Microsoft.Net PlatformPart of Microsoft.Net Platform ASP.net pages have a new file ASP.net pages have a new file
extension, .aspxextension, .aspx Better Language supportBetter Language support Has a large set of new controlsHas a large set of new controls Even – driven programmingEven – driven programming XML based programmingXML based programming
ASP.Net Key elementsASP.Net Key elements
1.1. Text Box ControlText Box Control
2.2. Radio Button ListRadio Button List
3.3. Check box controlCheck box control
4.4. Form and ButtonsForm and Buttons
5.5. Event ControlEvent Control
6.6. HyperLink ControlHyperLink Control
7.7. ArrayArray
8.8. BindingBinding
9.9. Hast TableHast Table
10.10. Data SetData Set
11.11. Database Connection and DisplayingDatabase Connection and Displaying
Define Text Boxes ControlDefine Text Boxes Control
My name is pan1. <asp:TextBox id="Box1"runat="server" />
2.My name is Pan. I am a student. My major is computer science.
<asp:TextBox id="Box2" TextMode="MultiLine" Columns="30" Rows="3" runat="server" />
3. ...................
<asp:TextBox id="Box3" TextMode="Password" runat="server" />
Define Radio Button List ControlDefine Radio Button List Control
RedRed
BlueBlue
GreenGreen
<asp:RadioButtonList id="MyButtons" runat="server"> <asp:ListItem Text="Red" /> <asp:ListItem Text="Blue" /> <asp:ListItem Text="Green" /></asp:RadioButtonList>
asp:CheckBox Controlasp:CheckBox Control
Click to Submit Click to Submit SubmittedSubmitted
<SCRIPT runat="server">Sub CheckedBox (Src As Object, Args As EventArgs) MyCheckBox.Text = "Submitted"End Sub</SCRIPT>
<html><body><form runat="server">
<asp:CheckBox id="MyCheckBox" Text="Click to Submit" TextAlign="Right" Checked="False" AutoPostBack="True" OnCheckedChanged="CheckedBox" runat="server" /></form></body></html>
Forms and ButtonsForms and Buttons <script runat="server"><script runat="server">
Sub submit(Source As Object, e As Sub submit(Source As Object, e As EventArgs)EventArgs) button1.Text="You clicked me!" button1.Text="You clicked me!"End SubEnd Sub</script></script>
<html><html><body><body>
<form runat="server"><form runat="server"><asp:Button id="button1" Text="Click me!" <asp:Button id="button1" Text="Click me!" runat="server" OnClick="submit"/>runat="server" OnClick="submit"/></form></form>
</body></body></html></html>
Click me!
You clicked me!
ASP.NET Event ControlASP.NET Event Control <script runat="server"> <script runat="server"> Sub Page_LoadSub Page_Load lbl1.Text="The date and time is " & now()lbl1.Text="The date and time is " & now() End Sub End Sub </script></script>
<html><html> <body> <body> <form runat="server"> <form runat="server"> <h3><asp:label id="lbl1" runat="server" /></h3><h3><asp:label id="lbl1" runat="server" /></h3> </form> </form> </body> </body> </html> </html>
The date and time is 24.11.2003 The date and time is 24.11.2003 06:36:2106:36:21
ASP.NET HyperLink ControlASP.NET HyperLink Control
Here is a text link to the Home PageHere is a text link to the Home Page
<asp:HyperLink id="Link1" style="font-size:12pt"<asp:HyperLink id="Link1" style="font-size:12pt" NavigateUrl=“www.google.com" NavigateUrl=“www.google.com" Text="Here is a text link to the Home Page" Text="Here is a text link to the Home Page" runat="server" /> runat="server" />
<asp:HyperLink id="Link2" style="font-size:12pt" NavigateUrl=“www.yahoo.com" ImageUrl="aspnet.gif" runat="server" />
Array in ASP.NETArray in ASP.NET
<SCRIPT runat="server">Sub Page_Load If Not IsPostBack Then
Dim ColorList = New ArrayList
ColorList.Add("Red") ColorList.Add("Green") ColorList.Add("Blue") ColorList.TrimToSize() ColorList.Sort() End IfEnd Sub</SCRIPT>
Blue
Green
red
Binding to an ArrayListBinding to an ArrayList Dim ColorList As New ArrayListDim ColorList As New ArrayList
ColorList.Add("Red")ColorList.Add("Red")ColorList.Add("Green")ColorList.Add("Green")ColorList.Add("Blue")ColorList.Add("Blue")ColorList.TrimToSize()ColorList.TrimToSize()ColorList.Sort()ColorList.Sort()RadioButtons.DataSource = ColorListRadioButtons.DataSource = ColorListRadioButtons.DataBindRadioButtons.DataBind
<!-- List control for bound data--><!-- List control for bound data--><asp:RadioButtonList id="RadioButtons" <asp:RadioButtonList id="RadioButtons" runat="server">runat="server">
ASP.NET Hash TableASP.NET Hash Table <script runat="server"><script runat="server"> sub Page_Load sub Page_Load if Not Page.IsPostBack then if Not Page.IsPostBack then dim mycountries=New Hashtable dim mycountries=New Hashtable
mycountries.Add("N","Norway") mycountries.Add("N","Norway") mycountries.Add("S","Sweden") mycountries.Add("S","Sweden") mycountries.Add("F","France") mycountries.Add("F","France") mycountries.Add("I","Italy") mycountries.Add("I","Italy") rb.DataSource=mycountries rb.DataSource=mycountries rb.DataValueField="Key" rb.DataValueField="Key" rb.DataTextField="Value" rb.DataTextField="Value" rb.DataBind() rb.DataBind()
end if end if end sub end sub sub displayMessage(s as Object,e sub displayMessage(s as Object,e
As EventArgs) As EventArgs)
lbl1.text="Your favorite country is: lbl1.text="Your favorite country is: " & rb.SelectedItem.Text " & rb.SelectedItem.Text
end sub end sub </script> </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>
FranceFrance
SwedenSweden
ItalyItaly
NorwayNorway
Your favorite country is: ItalyYour favorite country is: Italy
FranceFrance SwedenSweden ItalyItaly NorwayNorway
Your favorite country is: NorwayYour favorite country is: Norway
XML Files and DataSetXML Files and DataSet <?xml version="1.0" encoding="ISO-8859-1" ?> <?xml version="1.0" encoding="ISO-8859-1" ?> <!-- Edited with XML Spy v4.2 <!-- Edited with XML Spy v4.2 --> --> - <countries>- <countries> - <country>- <country> <text><text>NorwayNorway</text> </text> <value><value>NN</value> </value> </country></country> - <country>- <country> <text><text>SwedenSweden</text> </text> <value><value>SS</value> </value> </country></country> - <country>- <country> <text><text>FranceFrance</text> </text> <value><value>FF</value> </value> </country></country> - <country>- <country> <text><text>ItalyItaly</text> </text> <value><value>II</value> </value> </country></country> </countries></countries>
DatasetDataset
<%@ Import Namespace="System.Data" %><%@ Import Namespace="System.Data" %>
Sub Page_LoadSub Page_Load If Not IsPostBack Then If Not IsPostBack Then
Dim MenuItems = New DataSet Dim MenuItems = New DataSet MenuItems.ReadXml(MapPath(“test.xml")) MenuItems.ReadXml(MapPath(“test.xml"))
End If End IfEnd SubEnd Sub
Binding to controlBinding to control
<%@ Import Namespace="System.Data" %><%@ Import Namespace="System.Data" %>
<script runat="server"><script runat="server">sub Page_Loadsub Page_Loadif Not Page.IsPostBack thenif Not Page.IsPostBack then dim mycountries=New DataSet dim mycountries=New DataSet mycountries.ReadXml(MapPath("countries.xml")) mycountries.ReadXml(MapPath("countries.xml")) rb.DataSource=mycountries rb.DataSource=mycountries rb.DataValueField="value" rb.DataValueField="value" rb.DataTextField="text" rb.DataTextField="text" rb.DataBind() rb.DataBind()end ifend ifend subend sub
sub displayMessage(s as Object,e As EventArgs)sub displayMessage(s as Object,e As EventArgs)lbl1.text="Your favorite country is: " & rb.SelectedItem.Textlbl1.text="Your favorite country is: " & rb.SelectedItem.Textend subend sub</script></script>
Binding to a controlBinding to a control
<html><html><body><body>
<form runat="server"><form runat="server"><asp:RadioButtonList id="rb" runat="server"<asp:RadioButtonList id="rb" runat="server"AutoPostBack="True" AutoPostBack="True" onSelectedIndexChanged="displayMessage" />onSelectedIndexChanged="displayMessage" /><p><asp:label id="lbl1" runat="server" /></p><p><asp:label id="lbl1" runat="server" /></p></form></form>
</body></body></html></html>
ASP.NET Database ConnectionASP.NET Database Connection
<%# Import Namespace="System.Data.OleDb" %><%# Import Namespace="System.Data.OleDb" %>
OleDbConnectionOleDbConnection - for connecting to a - for connecting to a database database
OleDbCommandOleDbCommand - for issuing SQL queries - for issuing SQL queries against database tables against database tables
OleDbDataReaderOleDbDataReader - for access to the - for access to the datasets retrieved from queriesdatasets retrieved from queries
Opening a Database ConnectionOpening a Database Connection
<%@ Import Namespace="System.Data.OleDb" %><%@ Import Namespace="System.Data.OleDb" %>
<SCRIPT runat="server"><SCRIPT runat="server">Sub Page_LoadSub Page_Load
'Open database connection 'Open database connection
Dim DBConnection = New Dim DBConnection = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;
DATA SOURCE=d:\Databases\eCommerce.mdb")DATA SOURCE=d:\Databases\eCommerce.mdb") DBConnection.Open() DBConnection.Open()
End Sub End Sub </SCRIPT> </SCRIPT>
Creating a Database CommandCreating a Database Command <%@ Import Namespace="System.Data.OleDb" %><%@ Import Namespace="System.Data.OleDb" %>
<SCRIPT runat="server"><SCRIPT runat="server">Sub Page_LoadSub Page_Load
'Open database connection 'Open database connection
Dim DBConnection = New Dim DBConnection = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0; DATA SOURCE=d:\Databases\eCommerce.mdb") DATA SOURCE=d:\Databases\eCommerce.mdb") DBConnection.Open() DBConnection.Open()
'Create database command and issue through connection 'Create database command and issue through connection
Dim SQLString = "SELECT * FROM Products"Dim SQLString = "SELECT * FROM Products" Dim DBCommand = New OleDbCommand(SQLString, Dim DBCommand = New OleDbCommand(SQLString, DBConnection)DBConnection)
End SubEnd Sub</SCRIPT></SCRIPT>
Creating a DataReaderCreating a DataReader <%@ Import Namespace="System.Data.OleDb" %><%@ Import Namespace="System.Data.OleDb" %>
<SCRIPT runat="server"><SCRIPT runat="server">Sub Page_LoadSub Page_Load
'Open database connection 'Open database connection Dim DBConnection = New Dim DBConnection = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0; DATA SOURCE=d:\Databases\eCommerce.mdb") DATA SOURCE=d:\Databases\eCommerce.mdb") DBConnection.Open() DBConnection.Open()
'Create database command and issue through connection 'Create database command and issue through connection Dim SQLString = "SELECT * FROM Products" Dim SQLString = "SELECT * FROM Products" Dim DBCommand = New OleDbCommand(SQLString, Dim DBCommand = New OleDbCommand(SQLString, DBConnection)DBConnection)
'Return dataset to data reader 'Return dataset to data reader Dim DBReader = DBCommand.ExecuteReader() Dim DBReader = DBCommand.ExecuteReader()
End SubEnd Sub</SCRIPT></SCRIPT>
Display Database RecordsDisplay Database Records <%@ Import Namespace="System.Data.OleDb" %><%@ Import Namespace="System.Data.OleDb" %>
<script runat="server"><script runat="server">sub Page_Loadsub Page_Loaddim dbconn,sql,dbcomm,dbreaddim dbconn,sql,dbcomm,dbread
dbconn=New dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & server.mappath("northwind.mdb"))source=" & server.mappath("northwind.mdb"))dbconn.Open()dbconn.Open()
sql="SELECT * FROM customers"sql="SELECT * FROM customers"dbcomm=New OleDbCommand(sql,dbconn)dbcomm=New OleDbCommand(sql,dbconn)dbread=dbcomm.ExecuteReader()dbread=dbcomm.ExecuteReader()
customers.DataSource=dbreadcustomers.DataSource=dbreadcustomers.DataBind()customers.DataBind()
dbread.Close()dbread.Close()dbconn.Close()dbconn.Close()end subend sub</script></script>
Display DatabaseDisplay Database <html><html>
<body><body>
<form runat="server"><form runat="server"><asp:Repeater id="customers" <asp:Repeater id="customers" runat="server">runat="server">
<HeaderTemplate><HeaderTemplate><table border="1" <table border="1" width="100%">width="100%">
<tr bgcolor="#b0c4de"><tr bgcolor="#b0c4de"><th>Companyname</th><th>Companyname</th><th>Contactname</th><th>Contactname</th><th>Address</th><th>Address</th><th>City</th><th>City</th></tr></tr></HeaderTemplate></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>