A day with .Net

My day to day experince in .net

Archive for the ‘MCTS ASP.NET 3.5’ Category

State management in ASP.NET Part-2

Posted by vivekcek on July 29, 2009

2. Hidden Field
Hidden fields are just input fields that are embedded in HTML tag that is not visible to user. Hidden fields stores information of a single page like view state.
3. Cookies
A cookie is a small amount of data stored in client side, and passed with all page requests. There are two types of cookies persistent and temporary. Persistent cookies are stored in client side as a text file, and these cookies will not be destroyed even if the user shut down the system. So these cookies are used in a later time. But the temporary cookies are stored in browsers memory, which will be active for the current session only. For using cookies you have to add the System.Net name space.
An example code to implement Remember me feature in your login

Using System.Net;  

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            HttpCookie cookie = Request.Cookies["mycookie"];

            if (cookie != null)
            {
                string s = cookie["uname"].ToString();
                if (s == "vivek")
                {
                    Response.Redirect("Default2.aspx");
                }
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        HttpCookie cookie = new HttpCookie("mycookie");
        cookie["uname"] = txtCookie.Text;
        cookie.Expires = DateTime.Now.AddYears(1);
        Response.Cookies.Add(cookie);
    }

In the above code

cookie.Expires = DateTime.Now.AddYears(1);

Specifies the scope of cookie,Another properties avilable for setting cookie scope are.

cookie.Value=DateTime.Now.ToString();
cookie.Path="/MyFolder";
cookie.Domain="mysite.com";

4.Query String
Adding query string to URL from page1.aspx

Response.Redirect(page2.aspx?qid=" + 123 + "&tId=" + 34);

Acessing the query strings in page2.aspx

 string key = Request.QueryString["qid"];
 string _Tid = Request.QueryString["tId"]; ;

4. Application State
The ASP.NET application variables are not user specific. Application variables are initialized when the application started and destroys when server is restarted or IIS restarted. You can declare an application variable at the Application Start method in Global.asax file. Application variables are normally used to count application hits and finding number of users online.

protected void Page_Load(object sender, EventArgs e)
    {
        Application.Lock();
        int count = 0;
        if (Application["hit"].ToString() != string.Empty)
        {
            count = Convert.ToInt32(Application["hit"]);
        } 
            count++;
            Application["hit"] = count;
            Response.Write(count);
            Application.UnLock();
    }

 void Application_Start(object sender, EventArgs e) 
    {
        Application["hit"] = "";

    }

5.Session State

Session variables are user specific.Applicable to a single session,They are also declared in Global.asax as.

void Session_Start(object sender, EventArgs e) 
    {
        Session["uname"] = "";

    }

Every client that accesses the application has a different session and a distinct collection of information.it forces the web server to store additional information in memory. This extra memory requirement,even if it is small, can quickly grow to performance-destroying levels as thousands of clients access the site.

ASP.NET tracks each session using a unique 120-bit identifier

Advertisements

Posted in MCTS ASP.NET 3.5 | 1 Comment »

State Management in ASP.NET-Part 1

Posted by vivekcek on July 28, 2009

State management in ASP.NET concerns with tracking the user state in the application. Two types of state management in ASP.NET are client-side state management and server-side state management.
In the case of client side state management data is stored in user’s browser cache, or in browser cookies. But in the case of server side state management state is stored in server memory or in a database.
Various techniques used in state management are.
1: View State
View state is the default mechanism used by asp.net to store user specific request response data between page requests.
The Page.ViewState property returns a dictionary object for storing values in multiple requests of the same page.
When an asp.net page is processed the state of the page and its controls is hashed into a string and stored in a hidden field named _VIEWSTATE.

<input Type=”Hidden” name=”_VIEWSTATE” id=”_VIEWSTATE” value=”/jkhsdjkahdjsh5765”/>

View state is not secure; view state can be encrypted by ViewStateEncryptionMode property of a page
This will affect performance due to encryption and decryption. The view state encryption for an entire page can be set by the following code in the web.config file

<Configuration>
</system.web>
<pages ViewStateEncryptionMode=”always”/>
</system.web>
<Configuration>

Adding data to view state


this.ViewState.Add(“key”,”mydata”);

Retrieve data from view state

String myData=(string)ViewState[“key”];

The following code demonstrates a page that uses view state. It allows the user to save a set of values
(all the text that’s displayed in all the text boxes of a table) and restore it later. This example uses
recursive logic to dig through all child controls, and it uses the control ID for the view state key,
because this is guaranteed to be unique in the page.

protected void Button1_Click(object sender, EventArgs e)
    {
        SaveAllText(TABLE1.Controls, true);
        txtName.Text = "";
        txtPhone.Text = "";
    }

    private void SaveAllText(ControlCollection controls, bool saveNested)
    {

        foreach (Control control in controls)
        {
            if (control is TextBox)
            {
                // Store the text using the unique control ID.
                ViewState[control.ID] = ((TextBox)control).Text;
            }
            if ((control.Controls != null) && saveNested)
            {
                SaveAllText(control.Controls, true);
            }
        }
    
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        RestoreAllText(TABLE1.Controls, true);
    }
    private void RestoreAllText(ControlCollection controls, bool saveNested)
    { 
       foreach (Control control in controls)
           {
               if (control is TextBox)
                  {
                     if (ViewState[control.ID] != null)
                             ((TextBox)control).Text = (string)ViewState[control.ID];
                  }
                if ((control.Controls != null) && saveNested)
                 {
                     RestoreAllText(control.Controls, true);
                 }
           }
     }

Posted in MCTS ASP.NET 3.5 | Leave a Comment »

MCTS 70-562 Introduction

Posted by vivekcek on July 28, 2009

Yes today onwards i am starting a free training for MCTS 70-562 paper,70-562 includes the web application development with ASP.NET 3.5.Each day i will try to post some abstract of topics specified by Microsoft for this exam. I have no plan to cover the topic in syllabus order beginners please excuse me because this post will be helpful for guys having 1 year experience asp.net.Also I have latest dumps for all 3,5 frame work also e books of self paced training kits.

Posted in MCTS ASP.NET 3.5 | Leave a Comment »