c# - Page.ClientScript available 是否已在 class.cs 中使用?

标签 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 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,您就可以说:

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

关于c# - Page.ClientScript available 是否已在 class.cs 中使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11894219/

相关文章:

c# - 将连接字符串与后端代码分离

c# - 您如何获取 XML 文档以显示引用的 C# 库?

c# - 无法将类型 'System.Collections.Generic.List<String>' 隐式转换为 'System.Collections.Generic.IEnumerable<turon.Model.Products_Products>

c++ - 在类 : scoped_ptr or shared_ptr? 中将智能指针作为参数传递

C++类继承

c++ - 我必须创建一个对象才能在 C++ 中使用类方法吗?

c# - 文件 I/O 最佳实践 - byte[] 还是 FileStream?

c++ - 在类中访问 vector

java - 抽象类或接口(interface)

c# - 使用 CommandLine Parser Library 标记所需集