More Group Sites
Education Books
School Rankings
Jobless Net
Better Home
Enviro++
更好教育论坛


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

Notification

Icon
Error

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

Rank: Administration

Reputation:

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

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.

Code:
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);
or
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);
or
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, 24 July 2016 2:38:47 PM(UTC)  | Reason: Not specified

Sponsor
Rss Feed  Atom Feed
Users browsing this topic
Guest
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.