7

I want to add a reference to ScriptManager in my class library project instead ClientScriptManager, is it possible?

1
  • Can you rephrase the question? I don't understand what the issue is. ScriptManager is usually an ASP.NET Tag, not sure how you would add it in a Class Library. Commented Aug 8, 2011 at 21:38

1 Answer 1

11

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
Sign up to request clarification or add additional context in comments.

2 Comments

Ok! thanks! But i have a problem, i cannot acess the method RegisterClientScriptBlock in this way. Do you know why?
The RegisterClientScriptBlock method is static/shared. Hence this will work: System.Web.UI.ScriptManager.RegisterClientScriptBlock(page, page.GetType, "ScriptKey", "YourScript", True)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.