3
Answers

Session variable and Application variable

Mohit Kapoor

Mohit Kapoor

8y
821
1
Why value of Session variable and Application variable updates on page reload !!!
 
 
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace part_5_WebApplication2
{
public partial class Session1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["Click"] == null)
{
Session["Click"] = 0;
}
TextBox1.Text = Session["Click"].ToString();
}
}

protected void Button1_Click(object sender, EventArgs e)
{
int click_count = (int)Session["Click"] + 1;
TextBox1.Text = click_count.ToString();
Session["Click"] = click_count;
}

protected void TextBox1_TextChanged(object sender, EventArgs e)
{

}
}
}
------------------------------------------------------------------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Session1.aspx.cs" Inherits="part_5_WebApplication2.Session1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Click me !" />

</div>
</form>
</body>
</html>

Attachment: upload.zip

Answers (3)
0
Nilesh Sawardekar

Nilesh Sawardekar

397 3.9k 400.3k 8y
Your session variable have hold the value. and wheneverwe reload the page that time our info is resubmitted to site. Because of that your button1.click event get fire again so your getting incremented value.
use viewstate instead, if you don't want to increment it.
................................................................
accept as answer if it work for you.
Accepted
0
Mohit Kapoor

Mohit Kapoor

1.6k 94 21k 8y
yes yes increment
0
Nilesh Sawardekar

Nilesh Sawardekar

397 3.9k 400.3k 8y
update means, getting incremented?