SlideShare ist ein Scribd-Unternehmen logo
1 von 31
State Management
MUHAMMAD AMIR – A M I R 4IT@ GMAI L.COM
Why we need?
Stateless HTTP Protocol:
 No information about the request
 Request created/destroyed with each post backversion.
State Management in ASP.Net
State Management

Client Side!
Client Side – View State
 Mechanism that allows state values to be preserved across page postbacks
 EnableViewState property will be set to true

Advantages
 No server resources are required
 Simple implementation
 Enhanced security features

Disadvantages
 Performance considerations
 Device limitations
 Potential security risks
Client Side – View State
 Sample:

//To Save Information in View State
ViewState.Add ("NickName", "Dolly");
//Retrieving View state
String strNickName = ViewState ["NickName"];

 Code Example:
public int SomeInteger {
get {
object o = ViewState["SomeInteger"];
if (o != null) return (int)o;
return 0;
//a default
}
set { ViewState["SomeInteger"] = value; }
}

Code Sample
Client Side – View State
 Controls that use the view state
Client Side – Control State
 Essential for control to function properly
 It is Private viewstate for the control only
 Work even when ViewState turned off
Client Side – Control State

Code Sample

public class ControlStateWebControl : Control
{
private string _strStateToSave;
protected override void OnInit(EventArgs e)
{
Page.RegisterRequiresControlState(this);
base.OnInit(e);
}
protected override object SaveControlState() { return _strStateToSave; }
protected override void LoadControlState(object state)
{
if (state != null)
{
_strStateToSave = state.ToString();
}
}
}
Client Side – Hidden Fields
 Not rendered in browser
 Use to store small frequently changed data
Client Side – Hidden Fields

Code Sample

protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)|
Label1.Text = string.Format("Clicked {0} times", HiddenField1.Value);
}
protected void Button1_Click(object sender, EventArgs e)
{
HiddenField1.Value = (Convert.ToInt32(HiddenField1.Value) + 1).ToString();

}

Label1.Text = string.Format("Clicked {0} times", HiddenField1.Value);
Client Side – Cookies
 Stored at client side in text file or in memory of the client browser session
 Always sent with the request to the web server
 Read from user machine to identify user
Client Side – Cookies

Code Sample

//Storing value in cookie
HttpCookie cookie = new HttpCookie("NickName");
cookie.Value = "David";
Request.Cookies.Add(cookie);
//Retrieving value in cookie
if (Request.Cookies.Count > 0 && Request.Cookies["NickName"] != null)
lblNickName.Text = "Welcome" + Request.Cookies["NickName"].ToString();
else
lblNickName.Text = "Welcome Guest";
Client Side – Query String
 Used to pass information across pages
 Information passed along with URL in clear text
Client Side – Query String
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
string v = Request.QueryString["param"];
if (v != null)
{
Response.Write("param is ");
Response.Write(v);
}
string x = Request.QueryString["id"];
if (x != null)
{
Response.Write(" id detected");
}
}
}

Code Sample
Client Side – Summary
Client Side – Summary
State management

Recommended usage

View state

Use when you need to store small amounts of information for a page that will post back to itself. Using the ViewState
property provides functionality with basic security.

Control state

Hidden fields

Cookies

Use when you need to store small amounts of state information for a control between round trips to the server.
Use when you need to store small amounts of information for a page that will post back to itself or to another page,
and when security is not an issue.

Note
You can use a hidden field only on pages that are submitted to the server.
Use when you need to store small amounts of information on the client and security is not an issue.
Use when you are transferring small amounts of information from one page to another and security is not an issue.

Query string

Note
You can use query strings only if you are requesting the same page, or another page via a link.
State Management

Server Side!
State Management in ASP.Net
Server Side – Application State
 Used to share information among users
 Stored in the memory of the windows process
 Great place to store data that changes infrequently
Server Side – Application State
protected void Page_Load(object sender, EventArgs e)
{
int count = 0;
if (Application["HitCounterForOrderPage"] != null)
count = (int)Application["HitCounterForOrderPage"];
count++;
Application["HitCounterForOrderPage"] = count;
lblcounter.Text = "Page Visited: " + count.ToString() + "Times";
}

//Application.Lock(); and Application.UnLock();

Code Sample
Server Side – Application State
//Stroing information in application state
lock (this)
{
Application["NickName"] = "Nipun";
}
//Retrieving value from application state
lock (this)
{
string str = Application["NickName"].ToString();
}

Code Sample
Server Side – Session State
 Place to store values that will persist across page requests
 Values stored in Session are stored on the server
 Values remain in memory until they are explicitly removed or until the Session expires
Server Side – Session State
Sample:
//Storing informaton in session state
Session["NickName"] = "ABC";
//Retrieving information from session state
string str = Session["NickName"];
Code Example:

object sessionObject = Session["someObject"];
if (sessionObject != null)
{
myLabel.Text = sessionObject.ToString();
}

Code Sample
Server Side – Session State

Code Sample

In Process mode:
 Default mode
 Session values are stored in the web server's memory (in IIS & for each IIS Server)
 Values will be lost on server restart

<configuration>
<sessionstate mode="InProc" />
</configuration>
Server Side – Session State

Code Sample

In State Server mode:
 Store data out of AppPool
 Needs to serialize objects
 Values will not be lost on server restart
See more details

<configuration>
<sessionstate mode="stateserver"
stateConnectionString="tcpip=127.0.0.1:42424" />
</configuration>
Server Side – Session State

Code Sample

In SQL Server mode:
 Highly secure and reliable

 Overhead from serialization and deserialization
 Should be used when reliability is more important than performance
 Setup mode in sql server by running InstallSqlState.sql
Possible Path for .Net 4.0 [C:WindowsMicrosoft.NETFramework64v4.0.30319]
<configuration>
<sessionstate mode="sqlserver"
sqlConnectionString="Data Source=(local);User ID=sa;Password=pwd" />
</configuration>
Server Side – Profile Properties
 Allows you to store user-specific data
 Unlike session state, the profile data is not lost when a user's session expires
 Uses an ASP.NET profile, which is stored in a persistent format and associated with an
individual user
 Each user has its own profile
Server Side – Profile
Sample:
<profile>
<properties>
<add item="item name" />
</properties>
</profile>
Code Example:
<authentication mode="Windows" />
<profile>
<properties>
<add name="FirstName"/>
<add name="LastName"/>
<add name="Age"/>
<add name="City"/>
</properties>
</profile>

Code Sample
Server Side – Application State

Code Sample

State management

Recommended usage

Application state

Use when you are storing infrequently changed, global information that is used by many users, and security is not an
issue. Do not store large quantities of information in application state.

Session state

Use when you are storing short-lived information that is specific to an individual session and security is an issue. Do
not store large quantities of information in session state. Be aware that a session-state object will be created and
maintained for the lifetime of every session in your application. In applications hosting many users, this can occupy
significant server resources and affect scalability.

Profile properties

Use when you are storing user-specific information that needs to be persisted after the user session is expired and
needs to be retrieved again on subsequent visits to your application.

Database support

Use when you are storing large amounts of information, managing transactions, or the information must survive
application and session restarts. Data mining is a concern, and security is an issue.
Links
 http://msdn.microsoft.com/en-us/library/75x4ha6s.ASPX
 http://www.c-sharpcorner.com/uploadfile/nipuntomar/state-management-in-Asp-Net/
 http://www.c-sharpcorner.com/uploadfile/37db1d/hour-1-understanding-5-Asp-Net-statemanagement-techniques-in-5-hours/

with examples:
 http://www.codeproject.com/Articles/492397/State-Management-in-ASP-NET-Introduction
 http://msdn.microsoft.com/en-us/library/ff647327.aspx

Weitere ähnliche Inhalte

Was ist angesagt?

State management
State managementState management
State managementLalit Kale
 
State management
State managementState management
State managementteach4uin
 
ASP.NET 02 - How ASP.NET Works
ASP.NET 02 - How ASP.NET WorksASP.NET 02 - How ASP.NET Works
ASP.NET 02 - How ASP.NET WorksRandy Connolly
 
State management
State managementState management
State managementIblesoft
 
Programming web application
Programming web applicationProgramming web application
Programming web applicationaspnet123
 
Session 33 - Session Management using other Techniques
Session 33 - Session Management using other TechniquesSession 33 - Session Management using other Techniques
Session 33 - Session Management using other TechniquesPawanMM
 
Introduction to ajax
Introduction to ajaxIntroduction to ajax
Introduction to ajaxNir Elbaz
 
Data Access Options in SharePoint 2010
Data Access Options in SharePoint 2010Data Access Options in SharePoint 2010
Data Access Options in SharePoint 2010Rob Windsor
 
Session 30 - Servlets - Part 6
Session 30 - Servlets - Part 6Session 30 - Servlets - Part 6
Session 30 - Servlets - Part 6PawanMM
 
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?DicodingEvent
 

Was ist angesagt? (20)

AJAX
AJAXAJAX
AJAX
 
State management
State managementState management
State management
 
ASP.NET lecture 8
ASP.NET lecture 8ASP.NET lecture 8
ASP.NET lecture 8
 
State management
State managementState management
State management
 
Data Binding
Data BindingData Binding
Data Binding
 
ASP.NET Lecture 2
ASP.NET Lecture 2ASP.NET Lecture 2
ASP.NET Lecture 2
 
Understanding AJAX
Understanding AJAXUnderstanding AJAX
Understanding AJAX
 
Ajax
AjaxAjax
Ajax
 
Xml http request
Xml http requestXml http request
Xml http request
 
ASP.NET 02 - How ASP.NET Works
ASP.NET 02 - How ASP.NET WorksASP.NET 02 - How ASP.NET Works
ASP.NET 02 - How ASP.NET Works
 
State management
State managementState management
State management
 
Programming web application
Programming web applicationProgramming web application
Programming web application
 
AJAX
AJAXAJAX
AJAX
 
Session 33 - Session Management using other Techniques
Session 33 - Session Management using other TechniquesSession 33 - Session Management using other Techniques
Session 33 - Session Management using other Techniques
 
Introduction to ajax
Introduction to ajaxIntroduction to ajax
Introduction to ajax
 
Asp.net
Asp.netAsp.net
Asp.net
 
Data Access Options in SharePoint 2010
Data Access Options in SharePoint 2010Data Access Options in SharePoint 2010
Data Access Options in SharePoint 2010
 
Session 30 - Servlets - Part 6
Session 30 - Servlets - Part 6Session 30 - Servlets - Part 6
Session 30 - Servlets - Part 6
 
MVC.net training in pune
MVC.net training in pune MVC.net training in pune
MVC.net training in pune
 
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
 

Ähnlich wie State management

PCI Security Requirements - secure coding
PCI Security Requirements - secure codingPCI Security Requirements - secure coding
PCI Security Requirements - secure codingHaitham Raik
 
ASP.Net Presentation Part3
ASP.Net Presentation Part3ASP.Net Presentation Part3
ASP.Net Presentation Part3Neeraj Mathur
 
Bigger Stronger Faster
Bigger Stronger FasterBigger Stronger Faster
Bigger Stronger FasterChris Love
 
Developing your first application using FIWARE
Developing your first application using FIWAREDeveloping your first application using FIWARE
Developing your first application using FIWAREFIWARE
 
SharePoint Client Object Model (CSOM)
SharePoint Client Object Model (CSOM)SharePoint Client Object Model (CSOM)
SharePoint Client Object Model (CSOM)Kashif Imran
 
State management
State managementState management
State managementIblesoft
 
Web Technologies - forms and actions
Web Technologies -  forms and actionsWeb Technologies -  forms and actions
Web Technologies - forms and actionsAren Zomorodian
 
13 networking, mobile services, and authentication
13   networking, mobile services, and authentication13   networking, mobile services, and authentication
13 networking, mobile services, and authenticationWindowsPhoneRocks
 
Lyudmila Zharova: Developing Solutions for SharePoint 2010 Using the Client O...
Lyudmila Zharova: Developing Solutions for SharePoint 2010 Using the Client O...Lyudmila Zharova: Developing Solutions for SharePoint 2010 Using the Client O...
Lyudmila Zharova: Developing Solutions for SharePoint 2010 Using the Client O...SharePoint Saturday NY
 
Session viii(state mngtserver)
Session viii(state mngtserver)Session viii(state mngtserver)
Session viii(state mngtserver)Shrijan Tiwari
 
ASP.NET Overview - Alvin Lau
ASP.NET Overview - Alvin LauASP.NET Overview - Alvin Lau
ASP.NET Overview - Alvin LauSpiffy
 
RIA services exposing & consuming queries
RIA services exposing & consuming queriesRIA services exposing & consuming queries
RIA services exposing & consuming queriesiedotnetug
 
05 asp.net session07
05 asp.net session0705 asp.net session07
05 asp.net session07Vivek chan
 
The complete ASP.NET (IIS) Tutorial with code example in power point slide show
The complete ASP.NET (IIS) Tutorial with code example in power point slide showThe complete ASP.NET (IIS) Tutorial with code example in power point slide show
The complete ASP.NET (IIS) Tutorial with code example in power point slide showSubhas Malik
 
TechDays 2013 Jari Kallonen: What's New WebForms 4.5
TechDays 2013 Jari Kallonen: What's New WebForms 4.5TechDays 2013 Jari Kallonen: What's New WebForms 4.5
TechDays 2013 Jari Kallonen: What's New WebForms 4.5Tieturi Oy
 
Building the an End-to-End ASP.NET MVC 4, Entity Framework, HTML5, jQuery app...
Building the an End-to-End ASP.NET MVC 4, Entity Framework, HTML5, jQuery app...Building the an End-to-End ASP.NET MVC 4, Entity Framework, HTML5, jQuery app...
Building the an End-to-End ASP.NET MVC 4, Entity Framework, HTML5, jQuery app...Dan Wahlin
 
State management in ASP.NET
State management in ASP.NETState management in ASP.NET
State management in ASP.NETOm Vikram Thapa
 

Ähnlich wie State management (20)

PCI Security Requirements - secure coding
PCI Security Requirements - secure codingPCI Security Requirements - secure coding
PCI Security Requirements - secure coding
 
ASP.Net Presentation Part3
ASP.Net Presentation Part3ASP.Net Presentation Part3
ASP.Net Presentation Part3
 
2310 b 14
2310 b 142310 b 14
2310 b 14
 
Bigger Stronger Faster
Bigger Stronger FasterBigger Stronger Faster
Bigger Stronger Faster
 
Developing your first application using FIWARE
Developing your first application using FIWAREDeveloping your first application using FIWARE
Developing your first application using FIWARE
 
SharePoint Client Object Model (CSOM)
SharePoint Client Object Model (CSOM)SharePoint Client Object Model (CSOM)
SharePoint Client Object Model (CSOM)
 
State management
State managementState management
State management
 
Web Technologies - forms and actions
Web Technologies -  forms and actionsWeb Technologies -  forms and actions
Web Technologies - forms and actions
 
Bh Win 03 Rileybollefer
Bh Win 03 RileybolleferBh Win 03 Rileybollefer
Bh Win 03 Rileybollefer
 
13 networking, mobile services, and authentication
13   networking, mobile services, and authentication13   networking, mobile services, and authentication
13 networking, mobile services, and authentication
 
Lyudmila Zharova: Developing Solutions for SharePoint 2010 Using the Client O...
Lyudmila Zharova: Developing Solutions for SharePoint 2010 Using the Client O...Lyudmila Zharova: Developing Solutions for SharePoint 2010 Using the Client O...
Lyudmila Zharova: Developing Solutions for SharePoint 2010 Using the Client O...
 
Session viii(state mngtserver)
Session viii(state mngtserver)Session viii(state mngtserver)
Session viii(state mngtserver)
 
ASP.NET Overview - Alvin Lau
ASP.NET Overview - Alvin LauASP.NET Overview - Alvin Lau
ASP.NET Overview - Alvin Lau
 
RIA services exposing & consuming queries
RIA services exposing & consuming queriesRIA services exposing & consuming queries
RIA services exposing & consuming queries
 
05 asp.net session07
05 asp.net session0705 asp.net session07
05 asp.net session07
 
The complete ASP.NET (IIS) Tutorial with code example in power point slide show
The complete ASP.NET (IIS) Tutorial with code example in power point slide showThe complete ASP.NET (IIS) Tutorial with code example in power point slide show
The complete ASP.NET (IIS) Tutorial with code example in power point slide show
 
TechDays 2013 Jari Kallonen: What's New WebForms 4.5
TechDays 2013 Jari Kallonen: What's New WebForms 4.5TechDays 2013 Jari Kallonen: What's New WebForms 4.5
TechDays 2013 Jari Kallonen: What's New WebForms 4.5
 
Building the an End-to-End ASP.NET MVC 4, Entity Framework, HTML5, jQuery app...
Building the an End-to-End ASP.NET MVC 4, Entity Framework, HTML5, jQuery app...Building the an End-to-End ASP.NET MVC 4, Entity Framework, HTML5, jQuery app...
Building the an End-to-End ASP.NET MVC 4, Entity Framework, HTML5, jQuery app...
 
70562 (1)
70562 (1)70562 (1)
70562 (1)
 
State management in ASP.NET
State management in ASP.NETState management in ASP.NET
State management in ASP.NET
 

Kürzlich hochgeladen

Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 

Kürzlich hochgeladen (20)

Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 

State management

  • 1. State Management MUHAMMAD AMIR – A M I R 4IT@ GMAI L.COM
  • 2. Why we need? Stateless HTTP Protocol:  No information about the request  Request created/destroyed with each post backversion.
  • 5. Client Side – View State  Mechanism that allows state values to be preserved across page postbacks  EnableViewState property will be set to true Advantages  No server resources are required  Simple implementation  Enhanced security features Disadvantages  Performance considerations  Device limitations  Potential security risks
  • 6. Client Side – View State  Sample: //To Save Information in View State ViewState.Add ("NickName", "Dolly"); //Retrieving View state String strNickName = ViewState ["NickName"];  Code Example: public int SomeInteger { get { object o = ViewState["SomeInteger"]; if (o != null) return (int)o; return 0; //a default } set { ViewState["SomeInteger"] = value; } } Code Sample
  • 7. Client Side – View State  Controls that use the view state
  • 8. Client Side – Control State  Essential for control to function properly  It is Private viewstate for the control only  Work even when ViewState turned off
  • 9. Client Side – Control State Code Sample public class ControlStateWebControl : Control { private string _strStateToSave; protected override void OnInit(EventArgs e) { Page.RegisterRequiresControlState(this); base.OnInit(e); } protected override object SaveControlState() { return _strStateToSave; } protected override void LoadControlState(object state) { if (state != null) { _strStateToSave = state.ToString(); } } }
  • 10. Client Side – Hidden Fields  Not rendered in browser  Use to store small frequently changed data
  • 11. Client Side – Hidden Fields Code Sample protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack)| Label1.Text = string.Format("Clicked {0} times", HiddenField1.Value); } protected void Button1_Click(object sender, EventArgs e) { HiddenField1.Value = (Convert.ToInt32(HiddenField1.Value) + 1).ToString(); } Label1.Text = string.Format("Clicked {0} times", HiddenField1.Value);
  • 12. Client Side – Cookies  Stored at client side in text file or in memory of the client browser session  Always sent with the request to the web server  Read from user machine to identify user
  • 13. Client Side – Cookies Code Sample //Storing value in cookie HttpCookie cookie = new HttpCookie("NickName"); cookie.Value = "David"; Request.Cookies.Add(cookie); //Retrieving value in cookie if (Request.Cookies.Count > 0 && Request.Cookies["NickName"] != null) lblNickName.Text = "Welcome" + Request.Cookies["NickName"].ToString(); else lblNickName.Text = "Welcome Guest";
  • 14. Client Side – Query String  Used to pass information across pages  Information passed along with URL in clear text
  • 15. Client Side – Query String public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { string v = Request.QueryString["param"]; if (v != null) { Response.Write("param is "); Response.Write(v); } string x = Request.QueryString["id"]; if (x != null) { Response.Write(" id detected"); } } } Code Sample
  • 16. Client Side – Summary
  • 17. Client Side – Summary State management Recommended usage View state Use when you need to store small amounts of information for a page that will post back to itself. Using the ViewState property provides functionality with basic security. Control state Hidden fields Cookies Use when you need to store small amounts of state information for a control between round trips to the server. Use when you need to store small amounts of information for a page that will post back to itself or to another page, and when security is not an issue. Note You can use a hidden field only on pages that are submitted to the server. Use when you need to store small amounts of information on the client and security is not an issue. Use when you are transferring small amounts of information from one page to another and security is not an issue. Query string Note You can use query strings only if you are requesting the same page, or another page via a link.
  • 20. Server Side – Application State  Used to share information among users  Stored in the memory of the windows process  Great place to store data that changes infrequently
  • 21. Server Side – Application State protected void Page_Load(object sender, EventArgs e) { int count = 0; if (Application["HitCounterForOrderPage"] != null) count = (int)Application["HitCounterForOrderPage"]; count++; Application["HitCounterForOrderPage"] = count; lblcounter.Text = "Page Visited: " + count.ToString() + "Times"; } //Application.Lock(); and Application.UnLock(); Code Sample
  • 22. Server Side – Application State //Stroing information in application state lock (this) { Application["NickName"] = "Nipun"; } //Retrieving value from application state lock (this) { string str = Application["NickName"].ToString(); } Code Sample
  • 23. Server Side – Session State  Place to store values that will persist across page requests  Values stored in Session are stored on the server  Values remain in memory until they are explicitly removed or until the Session expires
  • 24. Server Side – Session State Sample: //Storing informaton in session state Session["NickName"] = "ABC"; //Retrieving information from session state string str = Session["NickName"]; Code Example: object sessionObject = Session["someObject"]; if (sessionObject != null) { myLabel.Text = sessionObject.ToString(); } Code Sample
  • 25. Server Side – Session State Code Sample In Process mode:  Default mode  Session values are stored in the web server's memory (in IIS & for each IIS Server)  Values will be lost on server restart <configuration> <sessionstate mode="InProc" /> </configuration>
  • 26. Server Side – Session State Code Sample In State Server mode:  Store data out of AppPool  Needs to serialize objects  Values will not be lost on server restart See more details <configuration> <sessionstate mode="stateserver" stateConnectionString="tcpip=127.0.0.1:42424" /> </configuration>
  • 27. Server Side – Session State Code Sample In SQL Server mode:  Highly secure and reliable  Overhead from serialization and deserialization  Should be used when reliability is more important than performance  Setup mode in sql server by running InstallSqlState.sql Possible Path for .Net 4.0 [C:WindowsMicrosoft.NETFramework64v4.0.30319] <configuration> <sessionstate mode="sqlserver" sqlConnectionString="Data Source=(local);User ID=sa;Password=pwd" /> </configuration>
  • 28. Server Side – Profile Properties  Allows you to store user-specific data  Unlike session state, the profile data is not lost when a user's session expires  Uses an ASP.NET profile, which is stored in a persistent format and associated with an individual user  Each user has its own profile
  • 29. Server Side – Profile Sample: <profile> <properties> <add item="item name" /> </properties> </profile> Code Example: <authentication mode="Windows" /> <profile> <properties> <add name="FirstName"/> <add name="LastName"/> <add name="Age"/> <add name="City"/> </properties> </profile> Code Sample
  • 30. Server Side – Application State Code Sample State management Recommended usage Application state Use when you are storing infrequently changed, global information that is used by many users, and security is not an issue. Do not store large quantities of information in application state. Session state Use when you are storing short-lived information that is specific to an individual session and security is an issue. Do not store large quantities of information in session state. Be aware that a session-state object will be created and maintained for the lifetime of every session in your application. In applications hosting many users, this can occupy significant server resources and affect scalability. Profile properties Use when you are storing user-specific information that needs to be persisted after the user session is expired and needs to be retrieved again on subsequent visits to your application. Database support Use when you are storing large amounts of information, managing transactions, or the information must survive application and session restarts. Data mining is a concern, and security is an issue.
  • 31. Links  http://msdn.microsoft.com/en-us/library/75x4ha6s.ASPX  http://www.c-sharpcorner.com/uploadfile/nipuntomar/state-management-in-Asp-Net/  http://www.c-sharpcorner.com/uploadfile/37db1d/hour-1-understanding-5-Asp-Net-statemanagement-techniques-in-5-hours/ with examples:  http://www.codeproject.com/Articles/492397/State-Management-in-ASP-NET-Introduction  http://msdn.microsoft.com/en-us/library/ff647327.aspx