是否Page.ClientScript提供class.cs被使用?(Does Page.Client

2019-09-21 02:44发布

我有一个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);
        }

    }
}

Answer 1:

因为它看起来像您正在使用不从派生类,您有问题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();


文章来源: Does Page.ClientScript available been used in class.cs?
标签: c# class