|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Register script block from app_codeIs it possible to register client script block from app_code function? public static void CheckSessionModal() { String scriptValue=""; scriptValue = "<script language=\"vbscript\">" + Environment.NewLine + "window.returnValue=-1"+ "window.close"+Environment.NewLine+"</script>"; Page.ClientScript.RegisterStartupScript(GetType(), "scriptName", scriptValue); } From every page in my program I call this function from page_onload event: functions.CheckSessionModal() But when I do this, I get an error message: Error 2 An object reference is required for the nonstatic field, method, or property 'System.Web.UI.Page.ClientScript.get' c:\inetpub\wwwroot\CP\App_Code\functions.cs 452 13 http://localhost/CP/ Error 3 An object reference is required for the nonstatic field, method, or property 'object.GetType()' c:\inetpub\wwwroot\CP\App_Code\functions.cs 452 53 http://localhost/CP/ How can I do that, any idea? Thank you, Simon Probably because "Page" doesn't exist, you can access it via code something
like: if (HttpContext.Current == null) { throw new Exception("Method must be called from a page context"); } Page page = HttpContext.Current.Handler as Page; if (page == null) { throw new Exception("Method must be called from a page context"); } you now have access to a "page" variable Karl Show quoteHide quote "SimonZ" <simon.zu***@studio-moderna.com> wrote in message news:eyQegTsNGHA.964@tk2msftngp13.phx.gbl... >I have function in my app_code folder. > > Is it possible to register client script block from app_code function? > > public static void CheckSessionModal() > > { > > String scriptValue=""; > > scriptValue = "<script language=\"vbscript\">" + Environment.NewLine + > "window.returnValue=-1"+ > > "window.close"+Environment.NewLine+"</script>"; > > Page.ClientScript.RegisterStartupScript(GetType(), "scriptName", > scriptValue); > > } > > > > From every page in my program I call this function from page_onload event: > > functions.CheckSessionModal() > > But when I do this, I get an error message: > > Error 2 An object reference is required for the nonstatic field, method, > or property 'System.Web.UI.Page.ClientScript.get' > c:\inetpub\wwwroot\CP\App_Code\functions.cs 452 13 http://localhost/CP/ > > Error 3 An object reference is required for the nonstatic field, method, > or property 'object.GetType()' c:\inetpub\wwwroot\CP\App_Code\functions.cs > 452 53 http://localhost/CP/ > > How can I do that, any idea? > > Thank you, > > Simon > > > > > when I call function, I add page and type as a parameter:
functions.CheckSessionModal(GetType(), Page) It works. But I guess, your way is better. Thanks, Simon "Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME Show quoteHide quote net> wrote in message news:eOJNZFuNGHA.456@TK2MSFTNGP15.phx.gbl... > Probably because "Page" doesn't exist, you can access it via code > something like: > > if (HttpContext.Current == null) > { > throw new Exception("Method must be called from a page context"); > } > > Page page = HttpContext.Current.Handler as Page; > if (page == null) > { > throw new Exception("Method must be called from a page context"); > } > > you now have access to a "page" variable > > Karl > > -- > http://www.openmymind.net/ > > > > "SimonZ" <simon.zu***@studio-moderna.com> wrote in message > news:eyQegTsNGHA.964@tk2msftngp13.phx.gbl... >>I have function in my app_code folder. >> >> Is it possible to register client script block from app_code function? >> >> public static void CheckSessionModal() >> >> { >> >> String scriptValue=""; >> >> scriptValue = "<script language=\"vbscript\">" + Environment.NewLine + >> "window.returnValue=-1"+ >> >> "window.close"+Environment.NewLine+"</script>"; >> >> Page.ClientScript.RegisterStartupScript(GetType(), "scriptName", >> scriptValue); >> >> } >> >> >> >> From every page in my program I call this function from page_onload >> event: >> >> functions.CheckSessionModal() >> >> But when I do this, I get an error message: >> >> Error 2 An object reference is required for the nonstatic field, method, >> or property 'System.Web.UI.Page.ClientScript.get' >> c:\inetpub\wwwroot\CP\App_Code\functions.cs 452 13 http://localhost/CP/ >> >> Error 3 An object reference is required for the nonstatic field, method, >> or property 'object.GetType()' >> c:\inetpub\wwwroot\CP\App_Code\functions.cs 452 53 http://localhost/CP/ >> >> How can I do that, any idea? >> >> Thank you, >> >> Simon >> >> >> >> >> > >
Other interesting topics
Web.config session timeout
sorting xml data in server control ASP.NET 2.0 SmartNavigation vs MaintainScrollPositionOnPostback? Need to add Button to DataList How to Comment an WebControl Type.GetType() daab and mysql App Directory displaying time for Online examinations in webapplications. Code behind |
|||||||||||||||||||||||