But the requirement is such that you want to redirect the user immediately after timeout to login page before the user submits the page. In this article, I am going to explain how we can achieve this in ASP.NET.Before we see how to redirect the the user to login page after session timeout, we will see how to refresh any page after certain intervals. This will help me to clearly explain how to play with page redirection from client.
Refreshing any page after certain interval
For refreshing any page after certain interval, you need to use client side script only. You cant do this in server side, The reason being that the page has been served to the client, end of request. The web is stateless; until the user comes back and initiates another request the server can't do anything. So we need to do this refreshing activity from client side only. There are two ways to achieve this,
1. Using Window.setTimeout method
2. Using
Using Window.setTimeout method:
DHTML Window object has a method called "setTimeout" which evaluates an expression after a specified number of milliseconds has elapsed. With the help of this method, you can run the client side script window.location.href="somepage" to redirect page in the current window. SetTimeout accepts three parameters.
vCode | Required. Variant that specifies the function pointer or string that indicates the code to be executed when the specified interval has elapsed. | ||||||
iMilliSeconds | Required. Integer that specifies the number of milliseconds. | ||||||
sLanguage | Optional. String that specifies one of the following values:
|
You need to call this method in body load and start the timer( by calling setTimeout method). This timer will elapse depending upon the second parameter for this method. When it is elapsed, it will fire the script which is given as first parameter. So you need to add this to the body element onload method. For adding client side event to body tag in asp.net you need to follow this methos.
1. Declare the body tag in html window as server control by specifing runat attribute and give an id to that tag.
< /P>< /FONT>
2. In the code behind, declare this element as htmlgenericcontrol like this.
Protected WithEvents body As System.Web.UI.HtmlControls.HtmlGenericControl
3. Finally add the following code in your page_load event handler, this will add the client side handler for body tag.
body.Attributes.Add("onLoad", "window.setTimeout(""window.location.href='
This method will refresh the page to the specified location after 5 secs. The disadvantage of this method is, this might not work in some lower end browsers. So you need to go for other method i.e. by using Meta Tags.
Using
Another way for refreshing the page after certain interval is by using meta tag - Refresh. This tag specifies a delay in seconds before the browser automatically reloads the document. Optionally, specifies an alternative URL to load. Example
<
In ASP.NET, you can add headers in code behind using this method.
Response.AppendHeader("Refresh", "10; URL=.aspx")
This method will reload the current window after interval mentioned in the second parameter to the page which is mentioned as URL to refresh. In this case page will be refreshed after 10 seconds.
Redirecting User To Login Page after Session Timeouts.
Redirecting user to login page after session timeout is similar to refreshing the page after certain intervals method. Only thing which will differ is that calculating time after which the page has to be redirected. Hence time can be calculated using Session.timeout property which will give us session timeout value for that session. Add some grace timings to that value and redirect the user to the login page automatically.
Using Window.setTimeout method
body.Attributes.Add("onLoad", "window.setTimeout(""window.location.href='login.aspx'""," & (Session.Timeout * 60 * 1000) + 10000 & ");")
Using
Response.AppendHeader("Refresh", Convert.ToString((Session.Timeout * 60) + 10) & "; url="Login".aspx")
Both these methods will redirect the user to login page after session timeout + 10 seconds. This is how you can redirect the user to login page after session timeout without user interaction.
' These are the namespaces
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.IO
'Code for btnsubmit
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim intFileNameLength As Integer
Dim strFileNamePath As String
Dim strFileNameOnly As String
If Not (UploadFile.PostedFile Is Nothing) Then
strFileNamePath = UploadFile.PostedFile.FileName
intFileNameLength = InStr(1, StrReverse(strFileNamePath), "\")
strFileNameOnly = Mid(strFileNamePath, (Len(strFileNamePath) - intFileNameLength) + 2)
Dim paths = Server.MapPath("/excelreading/")
paths = paths & "Excel/"
'If File.Exists(paths & strFileNameOnly) Then
'lblMessage.Text = "Image of Similar name already Exist,Choose other name"
'Else
If UploadFile.PostedFile.ContentLength > 40000 Then
lblMessage.Text = "The Size of file is greater than 4 MB"
ElseIf strFileNameOnly = "" Then
Exit Sub
Else
strFileNameOnly = Session("AdminID") & "-" & Session("Acountry") & "-" & Format(Date.Today, "mm-dd-yyyy").Replace("/", "-") & ".xls"
UploadFile.PostedFile.SaveAs(paths & strFileNameOnly)
lblMessage.Text = "File Upload Success."
Session("Img") = strFileNameOnly
End If
End If
'End If
Dim myDataset As New DataSet()
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("/excelreading/") & "excel/" & strFileNameOnly & ";" & _
"Extended Properties=Excel 8.0;"
''You must use the $ after the object you reference in the spreadsheet
Dim myData As New OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn)
myData.TableMappings.Add("Table", "ExcelTest")
myData.Fill(myDataset)
DataGrid1.DataSource = myDataset.Tables(0).DefaultView
DataGrid1.DataBind()
End Sub
'end of btnsubmit code
'here is the code for btnexport
Private Sub btnExportToExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExportToExcel.Click
' Set the content type to Excel.
Response.ContentType = "application/vnd.ms-excel"
' Remove the charset from the Content-Type header.
Response.Charset = ""
' Turn off the view state.
Me.EnableViewState = False
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
' Get the HTML for the control.
DataGrid1.RenderControl(hw)
' Write the HTML back to the browser.
Response.Write(tw.ToString())
' End the response.
Response.End()
lblMessage.Text = "For any more information , feel free to contact...!!!"
End Sub
'end of btnexport code
{ 0 comments ... read them below or add one }
Post a Comment