Can you post the error
i think u may got null reference exception ,if session expired.
check first the session is containing "UserId" variable Like if(Session["UserId"] != null)
If(Session["UserId"] != null)
Object obj = Session["UserID"];
What are you trying to do exactly?
It looks like you are trying to ensure that the user goes to the Login page if the user is not logged in.
The best way to do this is to use the Session_Start event in Global.asax
In your project go to "Add New Item..." then select Global.asax
Go down to Session_Start
Put your code there that redirects the user to the login page.
It looks like you are trying to ensure that the user goes to the Login page if the user is not logged in.
Yes, ensure is the user is logged in,or logged in is time out
In the Page_Load use "Response.Write",a Button in a UpdatePanel,When Click it ,it also execute Page_Load Function ,in the "Page_Load" function,use Response.Write so may be happend errors.
for exaple.
a button in updatepanel ,it click function use "Response.Write("aaa");",when click it ,a dialog box display
I'm not sure what you mean.
This also depends on the Session Mode that you are using.
If you are using the standard InProc session mode then basically the session will never be timed out. The session will time out, but then it is automatically renewed also.
What happens is that if the session is timed out, then as soon as the next request is made by that session, a new session is created, whcih may be why you are having a problem, because the session is never null.
As I said, you should either use the Session_Start event in the Global.asax file, OR you can check for Session.IsNewSession.
Instead of checking for Session == null, check Session.IsNewSession.
for example:
<%@. Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("aa");
}
}
then click the button,What happened?
Do you know my question
You may use Global.aspx in which you can use Session_Start and Session_End event
How can i write the code in the Session_End function.
void Session_End(object sender, EventArgs e)
{
Server.Transfer("Login.aspx");
}
but ...
No comments:
Post a Comment