且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Page.ClientScript是否可用于class.cs中?

更新时间:2023-12-05 23:20:10

您遇到问题,因为它看起来像您使用的不是从 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();