ASP.Net State Management

State Management
1.       Hidden Tags
2.       Request variable
3.       Cookies
4.       URL Rewriting  default.aspx?session= Session.SessionID
5.       Session Tracking : match  Session.SessionID

    Cookies: If enabled in browser then work Eg:



    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim ck As New HttpCookie("myCookie", "vinod")
            Dim d As New Date
            d.AddDays(1)
            ck.Expires = d 'persist for 1 day
            Response.Cookies.Add(ck)
            Response.Redirect("cookie2.aspx")
        End Sub


    In cookies2.aspx access it like this:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Request.Cookies.Item("myCookie") Is Nothing Then
                Response.Write(Request.Cookies.Item("myCookie").Name)
                Response.Write(Request.Cookies.Item("myCookie").Value)
                Response.Write(Request.Cookies.Item("myCookie").Path)
            End If


        End Sub

    Request: Request.Form, Request.querystring, Request.Params

    For Request.forms remove runat=server and add action="default4.aspx" in Form Tag

    If Request.HttpMethod.Equals("Get") Then
                Dim v = Request.QueryString("mode")
            Else
                Dim v = Request.Form("t"'where t is the input name = "t" id ="t" in html
    End If



     Tracing: Its a debugging mechanism. To enable trace Add following line in your web.config file inside system.web replace ( and ) with < and >



    (trace enabled="true " pageOutput="false" requestLimit ="10" traceMode="SortByTime "/)





    pageOutput="True" if wanna show trace at client else use http:\url\trace.axd   on server to get trace detail. It also show server variables detail in bottom.



    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

            Trace.Write("Trace Method")
            Try
                Dim i, j, k
                i = 10
                k = 0
                j = i / k
            Catch ex As Exception
                Trace.Warn(e.ToString)
            End Try
        End Sub





    -Vinod Kotiya
    www.vinodkotiya.com

    Comments