Asked by:
session time out alert

Question
-
User-809753952 posted
I want to trigger a session time out alert, only if there are any web controls (like gridview, detailsview) in edit mode.
It must not be triggered, if the controls are in read only mode
Thursday, May 9, 2019 2:14 PM
All replies
-
User475983607 posted
Pretty simple. Just render a JavaScript timer when the the data bound control enters edit mode.
https://www.w3schools.com/jsref/met_win_settimeout.asp
You can either add the JavaScript as part of the HTML response or use the script manger in your C# handler.
Thursday, May 9, 2019 2:18 PM -
User665608656 posted
Hi mnmhemaj,
mnmhemaj
It must not be triggered, if the controls are in read only modewhat does the control stand for?
According to your description, I couldn’t understand your requirement clearly.
Do you mean you want the session time out alert only trigged when girdview is in edit mode.
As megards says, you could use timer control to achieve your requirement.
More details, you could refer to below codes:
Notice: I set Timer.Interval = 2000 directly to facilitate operation and display. If you want to use session to set interval of timer, you can set it like this: Timer1.Interval = Session.Timeout;
<form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> <asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Enabled="false" ></asp:Timer> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="500px" RowStyle-HorizontalAlign="Center" DataKeyNames="Id" OnRowEditing="grdOrder_RowEditing" OnRowUpdating="grdOrder_RowUpdating" OnRowCancelingEdit="grdOrder_RowCancelingEdit" CellPadding="4" ForeColor="#333333" GridLines="None"> <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> <Columns> <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" /> <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> <asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" /> <asp:CommandField ShowEditButton="True" /> </Columns> <EditRowStyle BackColor="#999999" /> <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> <SortedAscendingCellStyle BackColor="#E9E7E2" /> <SortedAscendingHeaderStyle BackColor="#506C8C" /> <SortedDescendingCellStyle BackColor="#FFFDF8" /> <SortedDescendingHeaderStyle BackColor="#6F8DAE" /> </asp:GridView> </form>
Code behind:
public static string connectionString = ConfigurationManager.ConnectionStrings["TestConnectionString1"].ConnectionString; protected void Page_Load(object sender, EventArgs e) { Context.Session.Timeout = 1; if (!IsPostBack) { Bind(); } } private void Bind() { string strSql = "select * from Test"; using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand(strSql, connection)) { try { connection.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); } catch (SqlException ex) { throw new Exception(ex.Message); } finally { connection.Close(); } } } } protected void grdOrder_RowEditing(object sender, GridViewEditEventArgs e) { Timer1.Interval = 2000;//Timer.Interval = Session.Timeout; Timer1.Enabled = true; GridView1.EditIndex = e.NewEditIndex; Bind(); } protected void grdOrder_RowUpdating(object sender, GridViewUpdateEventArgs e) { string SQLString = "update Test set Name =@Name,Gender=@Gender where Id =@Id"; SqlParameter[] cmdParms = { new SqlParameter("@Name",((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim()), new SqlParameter("@Gender",((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim()), new SqlParameter("@Id",GridView1.DataKeys[e.RowIndex].Value.ToString())}; using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand(SQLString, connection)) { if (cmdParms != null) { foreach (SqlParameter parameter in cmdParms) { cmd.Parameters.Add(parameter); } } try { connection.Open(); cmd.ExecuteNonQuery(); } catch (SqlException ex) { throw ex; } finally { GridView1.EditIndex = -1; Bind(); Timer1.Enabled = false; connection.Close(); } } } } protected void grdOrder_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; Bind(); Timer1.Enabled = false; } protected void Timer1_Tick(object sender, EventArgs e) { ClientScriptManager cs = Page.ClientScript; if (!cs.IsStartupScriptRegistered(this.GetType(), "PopupScript")) { String cstext1 = "alert('You have timed out!');"; cs.RegisterStartupScript(this.GetType(), "PopupScript", cstext1, true); } //Timer1.Enabled = false; }
Here is the result picture:
Best Regards,
YongQing.
Friday, May 10, 2019 8:20 AM -
User-2054057000 posted
You can simply redirect users to login page if session is timed out.
The following code in web.config will do your work:
<authentication mode="Forms"> <forms defaultUrl="~/Default.aspx" loginUrl="~/Login.aspx" slidingExpiration="true" timeout="60" /> </authentication>
This answer is there is stackoverflow.
Saturday, May 11, 2019 4:43 AM -
User-1038772411 posted
Hi, mnmhemaj
JS Code :
<script language="javascript" type="text/javascript"> var sessionTimeoutWarning = "<%= System.Configuration.ConfigurationSettings.AppSettings ["SessionWarning"].ToString()%>"; var sessionTimeout = "<%= Session.Timeout %>"; var sTimeout = parseInt(sessionTimeoutWarning) * 60 * 1000; setTimeout('SessionWarning()', sTimeout); function SessionWarning() { var message = "Your session will expire in another " + (parseInt(sessionTimeout) - parseInt(sessionTimeoutWarning)) + " mins! Please Save the data before the session expires"; alert(message); } </script>
this Article contains all things what you need for your requirement. Please Refere Once you will Get as you want.
https://www.codeproject.com/Articles/227382/Alert-Session-Time-out-in-ASP-Net
Monday, May 13, 2019 7:43 AM