times!"> times!">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Working With ASP.NET Application. Create a new virtual directory The procedure to create a new virtual directory Internet Services Manager Right click.

Similar presentations


Presentation on theme: "Working With ASP.NET Application. Create a new virtual directory The procedure to create a new virtual directory Internet Services Manager Right click."— Presentation transcript:

1 Working With ASP.NET Application

2 Create a new virtual directory 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))

3 Using Application State Sub Page_Load Application( "PageCounter" ) += 1 lblCount.Text = Application( "pageCounter" ) End Sub BadPageCounter.aspx This page has been requested: <asp:Label ID="lblCount" Runat="Server" /> times!

4 Using Application State Sub Page_Load Application.Lock Application( "PageCounter" ) += 1 lblCount.Text = Application( "pageCounter" ) Application.Unlock End Sub GoodPageCounter.aspx This page has been requested: <asp:Label ID="lblCount" Runat="Server" /> times!

5 Using Application State Application.Remove( “ myItem ” ) Application.RemoveAll()

6 Using the Global.asax File 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" ) = dstProducts End Sub Note: Page.Cache (for.aspx) vs. Context.Cache (For Global.asax)

7 Using the Global.asax File Sub Page_Load dgrdProducts.DataSource = Cache( "Products" ) dgrdProducts.DataBind() End Sub DisplayProducts.aspx <asp:DataGrid ID="dgrdProducts" Runat="Server" />

8 Creating and Reading Session Cookies 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 Sub Sub Page_PreRender( s As Object, e As EventArgs ) Dim strKey As String For each strKey in Request.Cookies lblCookies.Text &= " " & strKey & "=" & _ Request.Cookies( strKey ).Value Next End Sub Note: session cookie is added to the browser ’ s memory but not recorded to a file

9 Creating and Reading Session Cookies CreateCookie.aspx Cookie Name: <asp:TextBox ID="txtCookieName" Runat="Server" /> Cookie Value: <asp:TextBox ID="txtCookieValue" Runat="Server" /> <asp:Button Text="Add Cookie!" OnClick="Button_Click" Runat="Server" /> Existing Cookies: <asp:Label ID="lblCookies" EnableViewState="False" Runat="Server" />

10 Creating and Reading Persistent Cookies Dim objCookie As New HttpCookie( “ myCookie ”, “ Hello!! ” ) objCookie.Expires=#12/25/2005# Response.Cookies.Add(objCookie) Dim objCookie As New HttpCookie( “ myCookie ”, “ Hello!! ” ) objCookie.ExpiresdateTime.MaxValue Response.Cookies.Add(objCookie)

11 Create 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( “ ” & strItem & “ = “ & objCookie.Values(strItem)) Next End If Note: objCookie.Values( “ color ” )= “ Red ” keyvalue

12 Using Session State Adding Items to Session State Session( “ MyItem ” ) = “ Hello!! ” Session( “ myDataSet ” ) = dstDataSet Response.Write(Session( “ myItem ” ))

13 Using Session State Removing Items From Session State Session.Remove( “ muItem ” )

14 Using 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)

15 Using Session State Ending a User Session Session.Timeout = 10 (not request a page for more than 10 minutes) Session.Abandon

16 Handling Session Events Sub Session_Start() If Application( "SessionCount" ) Is Nothing Then Application( "SessionCount" ) = 0 End If Application( "SessionCount" ) += 1 End Sub Sub Session_End() Application( "SessionCount" ) -= 1 End Sub Global.asax


Download ppt "Working With ASP.NET Application. Create a new virtual directory The procedure to create a new virtual directory Internet Services Manager Right click."

Similar presentations


Ads by Google