I have 2 pages on my dummy site in asp.net, (default.aspx and default2.aspx), On default.aspx, i created session like below
protected void Page_Load(object sender, EventArgs e)
{
Session["MySession"] = "WELCOME";
Session.Timeout = 1;
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("default2.aspx");
}
and on default2.aspx
if (Session["MySession"] != null)
Response.Write(Session["MySession"]);
else
Response.Write("Session Timed Out");
i was wondering that after 1 min the session will get erase, as i have timeout, but after one minute when i click on the button it redirected me to default2.aspx, and displayed a session value "WELCOME". can anyone tell me how to erase session value after particular duration
In your Default.aspx you have to check if it is not a post back otherwise the session will be initialized again for each button click
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.isPostBack())
{
Session["MySession"] = "WELCOME";
Session.Timeout = 1;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("default2.aspx");
}
Related
I was trying to perform Session State in ASP.NET, but it always start from 1 in different pages.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["count"] = 0;
}
int count = (int)Session["count"];
count++;
Label1.Text = count.ToString();
Session["count"] = count;
}
// In Global.asax
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
}
void Session_Start(object sender, EventArgs e)
{ // start of Session
Session["count"] = 0;
}
void Session_End(object sender, EventArgs e)
{
}
}
I expect the count continues but not starts from 1 after re-visit the webform. Thanks!
if you want to display the total active user on your site, use application object instead of session object and on session_start you increase count and on session_end, decrease count.
protected void Application_Start(object sender, EventArgs e)
{
Application["count"] = 0;
}
void Session_Start()
{ // start of Session
Application["count"] += 1;
}
void Session_End()
{
Application["count"] -= 1;
}
I have a MasterPage, and a Content page.
Whenever a method is called on the content page that does a redirect, EG:
protected void DoLogin(object sender, EventArgs e)
{
var result = AttemptLogin();
if (result.Success)
{
Response.Redirect("~/home");
}
else
{
... show error message
}
}
It appears Page_LoadComplete is not fired. The master page is set up as:
protected void Page_Init(object sender, EventArgs e)
{
Page.LoadComplete += Page_LoadComplete;
}
void Page_LoadComplete(object sender, EventArgs e)
{
DoSomething();
}
Should a redirect be stopping this from firing?
Clickhere Global.asax.cs
namespace WebApplication7
{
public class Global : System.Web.HttpApplication
{
private static int countdays=0;
protected void Application_Start(object sender, EventArgs e)
{
countdays = 0;
}
protected void Session_Start(object sender, EventArgs e)
{
countdays += 1;
}
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)
{
countdays -= 1;
}
protected void Application_End(object sender, EventArgs e)
{
}
public static int CountNo { get { return countdays; } }
}
}
Global.apsx
<body>
<form id="fromHitCounter" method="post" runat="server">
Total number of days since the Web server started:
<asp:label id="lblCount" runat="server"></asp:label><br />
</form>
</body>
Global.aspx.cs
private void Page_Load(object sender, System.EventArgs e)
{
int Countdays = HitCounters.Global.Countdays;//Hit counter does not exist
lblCount.Text = Countdays.ToString();
}
How to calculate days counter in asp.net using global.asax file,In Global.aspx.cs iam getting the error hit counter does not exist in the current context
I'm not going to ask why, I probably won't like the reason you give me. However you're not counting days here. You are counting session starts.
What you really want to do is something like this:
public class Global : System.Web.HttpApplication
{
private static DateTime started;
private static int days;
protected void Application_Start(object sender, EventArgs e)
{
started = DateTime.UtcNow;
days = 0;
}
protected void Session_Start(object sender, EventArgs e)
{
TimeSpan ts = DateTime.UtcNow - started;
days = (int)ts.TotalDays;
}
...
}
}
However, that's assuming the session events fire and you're also overlooking the fact the applications can and do get unloaded, your application may not stay loaded even a single day.
Your link points to counting the number of website visits which isn't the same as counting days or getting how long the web server has been running. It is also a very poor attempt because it does not take into account repeat visits from the same user etc and is not really persistent across app domain unloads.
protected void Page_Init(object sender, EventArgs e)
{ }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.DataSource = SomeObject.GetData();
DropDownList1.DataBind();
} }
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
var selvalue = DropDownList1.SelectedValue;
DoSomething(selvalue);
}
Please some body help me to make works properly this code if ViewState turned off.
If you turn the viewstate off, then you need to rebind the data on every trip to the server and before the page initializes.
Example of a dropdownlist without viewstate
I have this:
protected void Page_Load(object sender, EventArgs e)
{
nome_Txt.Text = Profile.dados_pessoais.nome;
}
protected void save_Click(object sender, EventArgs e)
{
Profile.dados_pessoais.nome = nome_Txt.Text;
}
If Profile.dados_pessoais.nome is empty, nome_txt.Text is empty too. When I change nome_Txt.Text to teste for example, when I click on the button nome_Txt.Text is empty.
What am I doing wrong?
The Page_Load event run before the button click event so you always assign the text box to empty value.
To solve this, don't populate the textbox when you are in a Postback:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
nome_Txt.Text = Profile.dados_pessoais.nome;
}
}
As also stated in a comment, you probably have to save the profile after changing it otherwise it won't be saved when you next load the page:
protected void save_Click(object sender, EventArgs e)
{
Profile.dados_pessoais.nome = nome_Txt.Text;
Profile.Save()
}