I want to add a reference to ScriptManager in my class library project instead ClientScriptManager, is it possible?
1 Answer
I assume that you don't know how to reference the ScriptManager in a class library where normally these WebControls are not referenced. Furthermore i think you also need to know how to get a reference to the page in a static context from the class library.
To get the ScriptManager you have to add a reference to System.Web.Extensions in your class library project.
To get a reference to the page in a static context you need to add the System.Web namespace, then following returns the ScriptManager of the current page:
C#:
var http = System.Web.HttpContext.Current;
if ((http != null)) {
var page = http.CurrentHandler as Web.UI.Page;
if (page != null) {
var scriptManager = System.Web.UI.ScriptManager.GetCurrent(page);
}
}
VB.NET:
Dim http = Web.HttpContext.Current
If Not http Is Nothing Then
Dim page = TryCast(http.CurrentHandler, Web.UI.Page)
If Not page Is Nothing Then
Dim scriptManager = System.Web.UI.ScriptManager.GetCurrent(page)
End If
End If
2 Comments
Michel Andrade
Ok! thanks! But i have a problem, i cannot acess the method RegisterClientScriptBlock in this way. Do you know why?
Tim Schmelter
The RegisterClientScriptBlock method is static/shared. Hence this will work:
System.Web.UI.ScriptManager.RegisterClientScriptBlock(page, page.GetType, "ScriptKey", "YourScript", True)