More Group Sites
Education Books
School Rankings
Jobless Net
Better Home

Help | Subscribe/Unsubscribe | Rules | Other Group Sites: Better Education | Better Education Forum
Welcome Guest! To enable all features please Login or Register.



Go to last post Go to first unread
#1 Posted : Sunday, July 24, 2016 1:51:51 PM(UTC)

Rank: Administration


Groups: AcademicCoachingSchool, admin, Administration, BookSeller, CatholicSchool, CoachingAdult, CoachingProfessional, CoachingSports, ExtraCurriculumCoaching, IndependentSchool, Moderator, MusicTeacher, PrivateSchool, PublicSchool, SelectiveSchool, tutor
Joined: 11/23/2008(UTC)
Posts: 521

RegisterStartupScript() vs RegisterClientScriptBlock()

The choice of which method to use really depends on the "order" in which you want your script to be run by the browser when rendering the page.
Put simply, if you want to call a javascript function or want to access controls on your page, use RegisterStartupScript(), else if you want to register javascript functions (a javascript function definition), use RegisterClientScriptBlock().

RegisterStartupScript: rendered after all the elements in the page before the closing tag of the Page object's <form runat="server"> element (right before the form's end tag </form>). This enables the script to call or reference page elements without the possibility of it not finding them in the Page's DOM. The code can access any of the form's elements because, at that time, the elements have been instantiated.

Two ways to use RegisterStartupScript and RegisterClientScriptBlock, for example:
1. On Page_Load or on Button Click (Page is Form Control)
Page.ClientScript.RegisterStartupScript(this.GetType(),  "activateMenuItem", "activateMenuItem();", true);
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),  "activateMenuItem", "activateMenuItem();", true);

2. ScriptManager for UpdatePanel, Async, in common place, page can also be "Page page = HttpContext.Current.Handler as Page;"
ScriptManager.RegisterStartupScript(page, page.GetType(), "activateMenuItem", "activateMenuItem();", true);
ScriptManager.RegisterClientScriptBlock(page, page.GetType(), "activateMenuItem", "activateMenuItem();", true);

The two ScriptManager methods RegisterStartupScript() and RegisterClientScriptBlock() are for Async UpdatePanel.

RegisterClientScriptBlock: rendered right after the ViewState tag, immediately below the opening tag of the Page object's <form runat="server"> element, i.e. before any of the page elements. It will immediately be executed by the browser. The code cannot access any of the form's elements because, at that time, the elements haven't been instantiated yet.

So it's easy to register or call any javascript function from code-behind on page load or on button click.

Edited by user Sunday, July 24, 2016 2:38:47 PM(UTC)  | Reason: Not specified

Rss Feed  Atom Feed
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.