Page.ClientScript是否可用于class.cs中? [英] Does Page.ClientScript available been used in class.cs?
问题描述
我有一个class.cs文件如下,但有一个错误,我的Page.ClientScript,其中说对象引用是非静态字段,方法或属性System.Web所必需的。 UI.Page.ClientScript.get'。所以我不知道是page.clientscript不可用于在class.cs?任何其他方法,而不是page.clientscript?
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);
}
}
}
推荐答案
您遇到问题,因为它看起来像您使用的不是从 System.Web.UI.Page
派生的类。但是,只要您可以访问 HttpContext
,就可以说:
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();
这篇关于Page.ClientScript是否可用于class.cs中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!