Can somebody explain for me what the differences are between ScriptManager and ClientScript?
ClientScript works well when I use it in Button_Clicked event, but it doesn’t work when I use it in the GridView_RowUpdated of a GridView. (The GirdView is wrapped inside an update panel). Then I tried ClientScript and it worked perfectly in this case.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You’ve pretty much identified the primary difference. The ScriptManager is meant to be used with async postbacks, which is why it works with the UpdatePanel. The ClientScript class is for synchronous postbacks. So, if you’re going to be posting back from an UpdatePanel, be sure to use the ScriptManager instead of ClientScript.
Method 2
9 years after this question was asked, I’m finding myself doing some software archeology and I’m writing up my own notes about ASP.NET’s idioms, hence this answer, which I hope actually answers the question, as I felt ShellyFM’s answer was incorrect, as this statement: “ClientScript class is for synchronous postbacks” is untrue)
ClientScriptManager
-
The
Page.ClientScriptproperty exposes an instance ofClientScriptManager.- Each
Pageinstance has its own single instance ofClientScriptManager. ClientScriptManagerstores a list of<script>elements. These<script>elements are automatically rendered inside the<asp:form runat="server">element, located immediately after where ViewState controls are rendered in<div class="aspNetHidden">.
- Each
-
These
<script>elements can be registered using different methods:RegisterClientScriptBlockRegisterClientScriptIncludeRegisterClientScriptResource
- For example:
-
RegisterClientScriptBlockadds an inline script directly to the page.// Page code: this.ClientScript.RegisterClientScriptBlock( type: typeof(TestPage), key: " Script1", script: "function foo(){}", addScriptTags: true );// *.aspx code: <form id="form1" runat="server"> </form>
// Rendered result: <form method="post" action="./TestPage.aspx" id="form1"> <div class="aspNetHidden"><!-- ViewState is rendered here --></div> <script type="text/javascript">function foo(){}</script> </form> -
RegisterClientScriptIncludewill add a<script src=""></script>element without any inline script: -
RegisterClientScriptResourcewill add a<script>that gets its content from an<EmbeddedResource>from a .NET Assembly’sGetManifestResourceStream. The script is referenced usingsrc="/WebResource.axd?d=..."instead of being rendered inline.- The
/WebResource.axdfile doesn’t exist in your filesystem. It’s a reserved URL pattern that ASP.NET handles by itself by-default.
- The
-
- The
Page.BeginFormRendermethod directly callsClientScriptManager.RenderClientScriptBlocks()and passes it theHtmlTextWriter, soClientScriptManageris not a WebControl.
-
Somewhat surprisingly,
ClientScriptManagerdoes not offer a way to remove or un-register a script – nor can you enumerate existing registrations: you can only replace an existing registration – and only if you know theString keythat was used to register it in the first place.
ScriptManager
-
The
ScriptManagerclass was added in ASP.NET AJAX which was an extension to ASP.NET 2.0 released in 2007.- It was built-in to ASP.NET in ASP.NET 4.0 in 2010 (instead of being an extension).
- It lives in
System.Web.Extensions.dll(in both ASP.NET 2.0 and 4.0), whileClientScriptManagerlives inSystem.Web.dll, which indicates it’s more central and integral to ASP.NET thanScriptManager. ScriptManageritself is a WebControl (it derives fromSystem.Web.UI.Control) so it’s responsible for rendering HTML directly via its.Render()method, whereasClientScriptManagerwas invoked directly byPage‘sBeginFormRender.
-
The
ScriptManagerdoes not have any instance methods for registering<script>elements to render to the page.- It does have
staticmethods for registering scripts to aPageinstance, but all it does is call intoPage.ClientScript.RegisterClientScriptBlock,Page.ClientScript.RegisterClientScriptInclude, orPage.ClientScript.RegisterClientScriptResource.
- It does have
-
The
<asp:ScriptManager>control must be placed inside your<asp:Form>and must be placed before any other WebControls that depend onScriptManager-registered scripts. You also cannot have more than 1<asp:ScriptManager>control on a page. -
So if
ScriptManagersimply wrapsClientScriptManager, what does it actually do itself?- Well, unlike
ClientScriptManager, theScriptManagerdoes let you remove a script registration and get a list of current registrations.- …provided those scripts were registered with
ScriptMangerand notClientScriptManagerand that you removed the offending script beforeScriptManager.RegisterScripts()is called (which happens insidePage‘sPreRenderevent btw).
- …provided those scripts were registered with
- Additionally,
ScriptManagerhandles the automatic creation of JavaScript code for ASMX and WCF client proxies and renders those as registered scripts too.- This is what the
<asp:ScriptManager><Services>collection is for. For each<asp:ServiceReference />child element it will generate a<script>containing functions that wrapXMLHttpRequestcalls to each[WebMethod]method.[WebMethod]methods exist in.asmxclasses, though yourWebServicesubclass also needs[ScriptService]applied.[WebMethod]can also bestaticmethods on yourPagesubclass, though you need to setEnablePageMethods="true"to use those.
- This is what the
- Well, unlike
TL;DR:
ClientScriptManageris integral to ASP.NET WebForms and renders pre-registered<script>elements inside your<form>.ScriptManageris an optional extension to ASP.NET WebForms (as a part of ASP.NET AJAX) and essentially extendsClientScriptManagerto allow for removing registrations and also generates JavaScript to make it easier to call[WebMethod]methods defined in.asmxWebServiceor a “Page method” (which is astaticmethod on aPagesubclass, also with[WebMethod]).
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0