What is state management? Explain Application state and Viewstate with suitable example.



Application State Example

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void btn_Click(object sender, System.EventArgs e) 
{  
	int counter = 0;  
    if(Application["counter"] !=null)  
    {  
    	counter = (int)Application["counter"];  
    }  
    counter++;  
    Application["counter"] = counter;  
    label.Text = "Counter : " + counter.ToString();  
}  
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Application state example in ASP.NET</title>
</head>
<body>
<form id="form1" runat="server">
  <div>
    <asp:Label ID="label" runat="server" Font-Size="Large" ForeColor="#000000" 
Text="Counter : 0"> </asp:Label>
    <br />
    <asp:Button ID="btn" runat="server" Text="Increment Counter" 
OnClick="btn_Click" />
  </div>
</form>
</body>
</html>

View State Example

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
if(ViewState["counter"] !=null)  
{  
	counter = (int)ViewState["counter"];  
	label.Text = "Counter : " + counter.ToString();  
}  
protected void btn_Click(object sender, System.EventArgs e) 
{
	if(ViewState["counter"] !=null)  
	{  
		counter = (int)ViewState["counter"];  
	}  
	else	
	{
		int counter = 0;  
	}
	counter++;  	
	ViewState["counter"] = counter;  
	label.Text = "Counter : " + counter.ToString();  
}  
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>View state example in ASP.NET</title>
</head>
<body>
<form id="form1" runat="server">
  <div>
    <asp:Label ID="label" runat="server" Font-Size="Large" ForeColor="#000000">
    </asp:Label>
    <br />
    <asp:Button ID="btn" runat="server" Text="Increment Counter" 
    OnClick="btn_Click" />
  </div>
</form>
</body>
</html>

Related Posts Plugin for WordPress, Blogger...

Engineering material

GTU IDP/ UDP PROJECT

GTU IDP/ UDP PROJECT

Patel free software download

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP