Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET - Using ScriptManager in class library reference

Tags:

c#

asp.net

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

like image 494
Michel Andrade Avatar asked Aug 08 '11 20:08

Michel Andrade


People also ask

Where do I put the ASP ScriptManager?

I would put the ScriptManager at the top of the Master page outside of any Multi-View. The ScriptManager is a what is used by the Microsoft controls to provide AJAX functionality.

What is the use of ScriptManager in asp net?

ScriptManager is a server-side control that sits on your Web Form and enables the core of ASP.NET AJAX. Its primary role is the arbitration of all other ASP.NET AJAX controls on the Web Form and the addition of the right scripting libraries to the Web browser so that the client portion of ASP.NET AJAX can function.

What is ScriptManager RegisterStartupScript in asp net?

RegisterStartupScript(Control, Type, String, String, Boolean) Registers a startup script block for a control that is inside an UpdatePanel by using the ScriptManager control, and adds the script block to the page.

What is ScriptManager How will you use it?

ScriptManager control registers the script for the Microsoft AJAX Library with the page. This enables client script support features such as partial-page rendering and Web-service calls. You must use a ScriptManager control on a page to enable the following features of ASP.NET AJAX: 1.


1 Answers

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
like image 189
Tim Schmelter Avatar answered Sep 21 '22 03:09

Tim Schmelter