I have a class.cs file as following, but there is an error for my "Page.ClientScript", which said "an object reference is required for the non-static field, method, or property 'System.Web.UI.Page.ClientScript.get'". So I. wonder is the page.clientscript is not available to used in class.cs? Any other method to use instead of page.clientscript ?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Security;
using System.Data;
using System.Web.Script.Services;
public class SessionExpired
{
public SessionExpired()
{
string csname = "timeoutWarning";
Type cstype = this.GetType();
if (!Page.ClientScript.IsStartupScriptRegistered(cstype, csname))
{
string strconfirm = "<script>" +
"window.setTimeout('SessionTimeOutHandler()', 60000);" +
"function SessionTimeOutHandler() { " +
"alert('Your login session is expired');" +
"window.location='../Processing.aspx';" +
"}</script>";
Page.ClientScript.RegisterStartupScript(cstype, csname, strconfirm, false);
}
}
}
You are having problems because it looks like you are using a class which doesn't derive from System.Web.UI.Page. However, as long as you have access to HttpContext, you can just say:
using System.Web;
using System.Web.UI;
...
var page = HttpContext.Current.CurrentHandler as Page;
if( page == null ){
// throw an exception, something bad happened
}
// now you have access to the current page...
page.ClientScript.RegisterStartupScript();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With