0% found this document useful (0 votes)
28 views

Session Management (AWT)

The document discusses session management in ASP.NET web applications. It provides code examples to demonstrate the use of ViewState, Hidden objects, Query strings and cookies to manage session state across multiple web pages. It also includes an example of a web application for college fest registration using cookies to maintain user sessions.

Uploaded by

s1062230126
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Session Management (AWT)

The document discusses session management in ASP.NET web applications. It provides code examples to demonstrate the use of ViewState, Hidden objects, Query strings and cookies to manage session state across multiple web pages. It also includes an example of a web application for college fest registration using cookies to maintain user sessions.

Uploaded by

s1062230126
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Assignment - 4 Session Management

Name :- Priyanka Gupta


Roll no:- 34

1. Write a program to demonstrate the ViewState.


Code :-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Assignment4
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

protected void Button1_Click(object sender, EventArgs e)


{
ViewState ["msg"] = TextBox1.Text;
}

protected void Button2_Click(object sender, EventArgs e)


{
Label2.Text = "You have entered " + ViewState["msg"].ToString();
}
}
}
2. Write a program to demonstrate the Hidden Object.

Code :-

WebForm2.aspx

<%@ Page Language="C#" AutoEventWireup="true"


CodeBehind="WebForm2.aspx.cs" Inherits="Assignment4.WebForm2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title></title>

</head>

<body>

<form id="form1" runat="server">

<div>

</div>

<asp:HiddenField ID="HiddenField1" runat="server" Value ="Vikash Gupta - 38"


OnValueChanged="HiddenField1_ValueChanged" />

<br />
<asp:Button ID="Button1" runat="server" Text="Show"
OnClick="Button1_Click" PostBackUrl="~/WebForm3.aspx" />

</form>

</body>

</html>

WebForm3.aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace Assignment4

public partial class WebForm3 : System.Web.UI.Page

protected void Page_Load(object sender, EventArgs e)

Response.Write(Request.Form["HiddenField1"].ToString());

}
3. Write a program to demonstrate the Query String.

WebForm1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"


Inherits="WebApplication6.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title></title>

</head>

<body>

<form id="form1" runat="server">

<div>

<div> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>


<br />

</div>

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Click" />

</div>

</form>

</body>

</html>

WebForm1.aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace WebApplication6

public partial class WebForm1 : System.Web.UI.Page

protected void Page_Load(object sender, EventArgs e)

protected void Button1_Click(object sender, EventArgs e)


{

Response.Redirect("WebForm2.aspx?msg=" + TextBox1.Text);

WebForm2.aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace WebApplication6

public partial class WebForm2 : System.Web.UI.Page

protected void Page_Load(object sender, EventArgs e)

if (Request.QueryString["msg"] != null)

Response.Write(Request.QueryString["msg"].ToString());

else

Response.Redirect("WebForm1.aspx");
}

Output :-

4. Create a web application to get following information from a user. Name, Age,
height, Email, Gender. Validate the input by using proper validation control. If the
gender is male then navigate to Male.aspx web page, and if the gender is female then
navigate to Female.aspx. Then depending upon the gender and height of the
individual suggest the ideal weight.

[Note : use cookies to pass information between pages]

The height to weight ratio is as follows

Height (CM) 150 160 170 180 190

Ideal weight for Male 60 65 70 75 80

Ideal weight for Female 55 60 65 70 75


WebForm1,aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace WebApplication7

public partial class WebForm1 : System.Web.UI.Page

protected void Page_Load(object sender, EventArgs e)

protected void Button1_Click(object sender, EventArgs e)

HttpCookie cookie = new HttpCookie("data");

cookie.Values ["Name"] = TextBox1.Text;

cookie.Values["Age"] = TextBox2.Text;

cookie.Values["Height"] = TextBox3.Text;

cookie.Values["Email"] = TextBox4.Text;

String gender = "";

if (Male.Checked ) { gender = "Male"; }


if (Female.Checked) { gender = "Female"; }

cookie.Values["gender"] = gender;

Response.Cookies.Add(cookie);

if (gender == "Male")

Response.Redirect("~/male.aspx");

else

Response.Redirect("~/Female.aspx");

male.aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace WebApplication7

public partial class male : System.Web.UI.Page

protected void Page_Load(object sender, EventArgs e)

int weight = 0;

String name = Request.Cookies["data"].Values["Name"];


String age = Request.Cookies["data"].Values["Age"];

String height = Request.Cookies["data"].Values["Height"];

String email = Request.Cookies["data"].Values["Email"];

if (int.Parse(height) == 150) { weight = 60; }

if (int.Parse(height) == 160) { weight = 65; }

if (int.Parse(height) == 170) { weight = 75; }

if (int.Parse(height) == 188) { weight = 80; }

if (int.Parse(height) == 190) { weight = 85; }

Response.Write("Name: " + name + " \n Age: " + age + "\n Height:" + height + "\n
Email: " + email + "\n Ideal Weight: " + weight);

Female.aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace WebApplication7

public partial class Female : System.Web.UI.Page

protected void Page_Load(object sender, EventArgs e)

{
int weight = 0;

String name = Request.Cookies["data"].Values["Name"];

String age = Request.Cookies["data"].Values["Age"];

String height = Request.Cookies["data"].Values["Height"];

String email = Request.Cookies["data"].Values["Email"];

if (int.Parse(height) == 150) { weight = 55; }

if (int.Parse(height) == 160) { weight = 60; }

if (int.Parse(height) == 170) { weight = 65; }

if (int.Parse(height) == 188) { weight = 70; }

if (int.Parse(height) == 190) { weight = 75; }

Response.Write("Name: " + name + " \n Age: " + age + "\n Height:" + height + "\n
Email: " + email + "\n Ideal Weight: " + weight);

5. Create an ASP.NET web application for your college fest. Provide login function
and registration for a few events. Use cookies to maintain the user's session.
WebForm.aspx

using System;

using System.Collections.Generic;

using System.Data.SqlClient;

using System.Linq;

using System.Net.NetworkInformation;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace WebApplication8

public partial class WebForm1 : System.Web.UI.Page

SqlConnection con = new SqlConnection(@"Data Source=(localdb)\


MSSQLLocalDB;Initial Catalog=EMPLOYEE;Integrated Security=True;");

protected void Page_Load(object sender, EventArgs e)

protected void Button1_Click(object sender, EventArgs e)

con.Open();

String Name = TextBox1.Text;


String Password = TextBox2.Text;

String Email = TextBox3.Text;

String Mobile = TextBox4.Text;

String EventName = DropDownList1.SelectedItem.ToString();

SqlCommand cmd = new SqlCommand("insert into Reg values('" + Name + "','"

+ Password + "','" + Email + "','"+ Mobile+"', '"+ EventName+"')",con);

int result = cmd.ExecuteNonQuery();

if (result > 0)

Response.Redirect("~/WebForm2.aspx");

else

Response.Write("something went wrong");

con.Close();

TextBox1.Text = String.Empty;

TextBox2.Text = String.Empty;

TextBox3.Text = String.Empty;

TextBox4.Text = String.Empty;

5_Session.aspx.cs

using System;
using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace WebApplication8

public partial class _5_Session : System.Web.UI.Page

protected void Page_Load(object sender, EventArgs e)

if (Session["user"]!= null)

Response.Write("Welcome to the college Fest " +

Session["user"].ToString() + "..... You are Successful");

else

Response.Redirect("~/WebForm2.aspx");

protected void TextBox2_TextChanged(object sender, EventArgs e)

{
}

protected void Button1_Click(object sender, EventArgs e)

protected void Button2_Click(object sender, EventArgs e)

Session.Abandon();

Session.Remove("user");

Response.Redirect("~/WebForm2.aspx");

WebForm2.aspx

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.Data;
namespace WebApplication8

public partial class WebForm2 : System.Web.UI.Page

SqlConnection conn = new SqlConnection(@"Data Source=(localdb)\


MSSQLLocalDB;Initial Catalog=EMPLOYEE;Integrated Security=True;Connect
Timeout=30;");

protected void Page_Load(object sender, EventArgs e)

protected void Button2_Click(object sender, EventArgs e)

conn.Open();

SqlCommand sqlCommand = new SqlCommand("select * from Reg where


username='"+TextBox1.Text+"' and password='"+TextBox2.Text+ "'",conn);

SqlDataAdapter adapter = new SqlDataAdapter(sqlCommand);

DataTable dataTable = new DataTable();

adapter.Fill(dataTable);

if (dataTable.Rows.Count > 0)

{
Session["user"] = TextBox1.Text;

Response.Redirect("~/5_Session.aspx");

}
6. Write a program to count the number of live users in your web application.

WebForm1.aspx

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace Program_6

public partial class WebForm1 : System.Web.UI.Page

protected void Page_Load(object sender, EventArgs e)

Label1.Text = "TOTAL Visitors" + Application["sessioncount"].ToString();


}

Global.asax.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.SessionState;

namespace Program_6

public class Global : System.Web.HttpApplication

protected void Application_Start(object sender, EventArgs e)

Application["sessioncount"] = 0;

protected void Session_Start(object sender, EventArgs e)

Application.Lock();

int count = (int)Application["sessioncount"];


Application["sessioncount"] = count + 1;

Application.UnLock();

protected void Application_BeginRequest(object sender, EventArgs e)

protected void Application_AuthenticateRequest(object sender, EventArgs e)

protected void Application_Error(object sender, EventArgs e)

protected void Session_End(object sender, EventArgs e)

Application.Lock();

int count = (int)Application["sessioncount"];

Application["sessioncount"] = count - 1;

Application.UnLock();
}

protected void Application_End(object sender, EventArgs e)

}
7. Write a web application to demonstrate page output caching.

WebForm1.aspx

<%@ Page Language="C#" AutoEventWireup="true"


CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication9.WebForm1" %>

<%@ OutputCache Duration="10" VaryByParam="None" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title></title>

</head>

<body>

<form id="form1" runat="server">

<div>

<h1>Output Caching</h1>

At the tone, the time will be:

<%=DateTime.Now.ToString() %>
</div>

</form>

</body>

</html>

8. Write a web application to display digital clock using ajax.

WebForm.1aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"


Inherits="WebApplication10.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

<title></title>

</head>

<body>

<form id="form1" runat="server">

<div>

</div>

<asp:ScriptManager ID="ScriptManager1" runat="server">

</asp:ScriptManager>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">

<ContentTemplate>

<asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick">

</asp:Timer>

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

</ContentTemplate>

<Triggers>

<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />

</Triggers>

</asp:UpdatePanel>

</form>

</body>

</html>

WebForm1.aspx.cs

using System;
using System.Collections.Generic;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace WebApplication10

public partial class WebForm1 : System.Web.UI.Page

protected void Page_Load(object sender, EventArgs e)

protected void Timer1_Tick(object sender, EventArgs e)

Label1.Text = DateTime.Now.ToString();

}
9. Write a web application to create an image slider using ajax.

1.

You might also like