Click here to Skip to main content
13,199,596 members (61,675 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I'm searching for an ans for the below quest,
You are implementing custom ASP.NET server controls.
Each control requires its own Client JavaScript code inorder to function properly.
The JavaScript uses functions that includes proper HTML elements for the control.
You need to ensure that the JavaScript for each of these controls that is used in ASP.NET is included in the generated HTML page only once,even if the ASP.NET page uses multiple instances of the given control,
Add the foll. Code line to the Page_Load method of each control, where classname is the name of the control class and strJavaScript contains the JavaScript Code for the control

a. Page.ClientScript.RegisterStartUpScript(typeof(ClassName),"script",strJavaScript)
b. Page.ClientScript.RegisterClientScriptBlock(typeof(ClassName),"script",strJavaScript)
Posted 3-Jan-13 1:38am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

a. When you use RegisterStartupScript, it will render your script after all the elements in the page (right before the form's end tag). This enables the script to call or reference page elements without the possibility of it not finding them in the Page's DOM.

b. When you use RegisterClientScriptBlock, the script is rendered right after the Viewstate tag, but before any of the page elements. Since this is a direct script (not a function that can be called, it will immediately be executed by the browser. But the browser does not find the label in the Page's DOM at this stage and hence you should receive an "Object not found" error.

For more information check this link[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171020.1 | Last Updated 3 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100