Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Options:

a. Page.ClientScript.RegisterStartUpScript(typeof(ClassName),"script",strJavaScript)
b. Page.ClientScript.RegisterClientScriptBlock(typeof(ClassName),"script",strJavaScript)
Posted

1 solution

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
http://stackoverflow.com/questions/666519/difference-between-registerstartupscript-and-registerclientscriptblock[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900