working with asp.net application. create a new virtual directory the procedure to create a new...
TRANSCRIPT
Working With ASP.NET Working With ASP.NET ApplicationApplication
Create a new virtual Create a new virtual directorydirectory
•The procedure to create a new virtual directory
Internet Services Manager
Right click default Web Site and choose New Virtual Directory
Provide the virtual directory with an alias
Choose a physical directory
Add a /bin directory (contain customer components and controls))
Using Application StateUsing Application State<Script Runat="Server">Sub Page_Load Application( "PageCounter" ) += 1 lblCount.Text = Application( "pageCounter" )End Sub</Script><html><head><title>BadPageCounter.aspx</title></head><body>This page has been requested:<asp:Label ID="lblCount" Runat="Server" />times!</body></html>
Using Application StateUsing Application State<Script Runat="Server">Sub Page_Load Application.Lock Application( "PageCounter" ) += 1 lblCount.Text = Application( "pageCounter" ) Application.UnlockEnd Sub</Script><html><head><title>GoodPageCounter.aspx</title></head><body>This page has been requested:<asp:Label ID="lblCount" Runat="Server" />times!</body></html>
Using Application StateUsing Application State
Application.Remove(“myIteApplication.Remove(“myItem”)m”)
Application.RemoveAll()Application.RemoveAll()
Using the Global.asax File Using the Global.asax File
<%@ Import Namespace="System.Data" %><%@ Import Namespace="System.Data.SqlClient" %><Script Runat="Server">Sub Application_Start Dim conNorthwind As SqlConnection Dim strSelect As String Dim dadProducts As SqlDataAdapter Dim dstProducts As DataSet conNorthwind = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;Database=Northwind" ) strSelect = "Select * From Products" dadProducts = New SqlDataAdapter( strSelect, conNorthwind ) dstProducts = New DataSet() dadProducts.Fill( dstProducts, "Products" ) Context.Cache( "Products" ) = dstProductsEnd Sub</Script>
Note: Page.Cache (for .aspx) vs. Context.Cache (For Global.asax)
Using the Global.asax File Using the Global.asax File
<Script Runat="Server">Sub Page_Load dgrdProducts.DataSource = Cache( "Products" ) dgrdProducts.DataBind()End Sub</Script><html><head><title>DisplayProducts.aspx</title></head><body><asp:DataGrid ID="dgrdProducts" Runat="Server" /></body></html>
Creating and Reading Session Creating and Reading Session CookiesCookies<Script Runat="Server">Sub Button_Click( s As Object, e As EventArgs ) Dim objCookie As HttpCookie objCookie = New HttpCookie( txtCookieName.Text, txtCookieValue.Text ) Response.Cookies.Add( objCookie )End SubSub Page_PreRender( s As Object, e As EventArgs ) Dim strKey As String For each strKey in Request.Cookies lblCookies.Text &= "<li>" & strKey & "=" & _ Request.Cookies( strKey ).Value NextEnd Sub</Script>
Note: session cookie is added to the browser’s memory but not recorded to a file
Creating and Reading Session CoCreating and Reading Session Cookiesokies
<html><head><title>CreateCookie.aspx</title></head><body><form Runat="Server">
<b>Cookie Name:</b><br><asp:TextBox ID="txtCookieName" Runat="Server" /><p><b>Cookie Value:</b><br><asp:TextBox ID="txtCookieValue" Runat="Server" /><p>
<asp:Button Text="Add Cookie!" OnClick="Button_Click" Runat="Server" />
<h2>Existing Cookies:</h2><asp:Label ID="lblCookies" EnableViewState="False" Runat="Server" />
</form></body></html>
Creating and Reading Persistent CCreating and Reading Persistent CookiesookiesDim objCookie As New HttpCookie(“myCookie”, “Hello!!”)objCookie.Expires=#12/25/2005#Response.Cookies.Add(objCookie)
Dim objCookie As New HttpCookie(“myCookie”, “Hello!!”)objCookie.ExpiresdateTime.MaxValueResponse.Cookies.Add(objCookie)
Create a Cookie DictionaryCreate a Cookie Dictionary
Dim objCookie As HttpCookies(“Preference”)objCookie.Values(“color”)=“Red”objCookie.Values(“fontface”)=“Arial”objCookie.Values(“fontsize”)=“4”Response.Cookies.Add(objCookie)
If objCookie.HasKeys Then Dim strItem as String For Each strItem in objCookie.Values Response.Write(“<Li>” & strItem & “=“ & objCookie.Values(strItem)) NextEnd If
Note: objCookie.Values(“color”)=“Red”
key value
Using Session StateUsing Session State
Adding Items to Session State
Session(“MyItem”) = “Hello!!” Session(“myDataSet”) = dstDataSet
Response.Write(Session(“myItem”))
Using Session StateUsing Session State
Removing Items From Session State
Session.Remove(“muItem”)
Using Session StateUsing Session State
Starting a User Session
When the first page is requested, the Web server adds the ASP.NET_SessionID Cookie to the user’s browser
Session.NewSession=True (when a new session start)
Response.Write(Session.SessionID)
Using Session StateUsing Session State
Ending a User Session
Session.Timeout = 10 (not request a page for more than 10 minutes)
Session.Abandon
Handling Session EventsHandling Session Events
<Script Runat="Server">
Sub Session_Start() If Application( "SessionCount" ) Is Nothing Then Application( "SessionCount" ) = 0 End If Application( "SessionCount" ) += 1End Sub
Sub Session_End() Application( "SessionCount" ) -= 1End Sub
</Script>
Global.asax