Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# class.cs中是否使用了可用的Page.ClientScript?_C#_Class - Fatal编程技术网

C# class.cs中是否使用了可用的Page.ClientScript?

C# class.cs中是否使用了可用的Page.ClientScript?,c#,class,C#,Class,我有一个class.cs文件,如下所示,但我的“Page.ClientScript”有一个错误,它说“非静态字段、方法或属性'System.Web.UI.Page.ClientScript.get'需要对象引用”。因此,我想知道page.clientscript是否不可用于class.cs?是否要使用其他方法代替page.clientscript using System; using System.Collections.Generic; using System.Linq; using Sy

我有一个class.cs文件,如下所示,但我的“Page.ClientScript”有一个错误,它说“非静态字段、方法或属性'System.Web.UI.Page.ClientScript.get'需要对象引用”。因此,我想知道page.clientscript是否不可用于class.cs?是否要使用其他方法代替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.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Data.SqlClient;
使用系统配置;
使用System.Web.Security;
使用系统数据;
使用System.Web.Script.Services;
公共类SessionExpired
{
公共会话expired()
{
string csname=“timeoutWarning”;
类型cstype=this.GetType();
如果(!Page.ClientScript.isstartupscript已注册(cstype,csname))
{
字符串strconfirm=“”+
“window.setTimeout('SessionTimeOutHandler()',60000);”+
“函数SessionTimeOutHandler(){”+
“警报('您的登录会话已过期');”+
“window.location='../Processing.aspx'+
"}";
Page.ClientScript.RegisterStartupScript(cstype,csname,strconfirm,false);
}
}
}

您遇到了问题,因为看起来您使用的类不是从
System.Web.UI.Page
派生的。但是,只要您有权访问
HttpContext
,您就可以说:

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();

我和你以前有同样的问题,请检查这个,希望它能解决你的问题,干杯。向你问好,迈克