HTML相关查询是否使用RegisterStartUpScript或RegisterClientScriptBlock [英] HTML related query whether to use RegisterStartUpScript or RegisterClientScriptBlock

查看:84
本文介绍了HTML相关查询是否使用RegisterStartUpScript或RegisterClientScriptBlock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索以下任务的ans,

您正在实现自定义ASP.NET服务器控件。

每个控件都需要自己的客户端JavaScript代码为了正常运行。

JavaScript使用包含适当控件的HTML元素的函数。

你需要确保每个控件的JavaScript用于ASP.NET只包含在生成的HTML页面中一次,即使ASP.NET页面使用给定控件的多个实例,

添加foll。每行控件的Page_Load方法的代码行,其中classname是控件类的名称,strJavaScript包含控件的JavaScript代码

选项:



a。 Page.ClientScript.RegisterStartUpScript(typeof(ClassName),script,strJavaScript)

b。 Page.ClientScript.RegisterClientScriptBlock(typeof(ClassName),script,strJavaScript)

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)

推荐答案

a。当您使用RegisterStartupScript时,它将在页面中的所有元素之后(在表单的结束标记之前)呈现您的脚本。这使得脚本可以调用或引用页面元素,而不会在页面的DOM中找不到它们。



b。使用RegisterClientScriptBlock时,脚本将在Viewstate标记之后,但在任何页面元素之前呈现。由于这是一个直接脚本(不是可以调用的函数,它将立即由浏览器执行。但是浏览器在此阶段没有在Page的DOM中找到标签,因此您应该收到一个对象找不到错误。



如需更多信息,请查看此链接

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


这篇关于HTML相关查询是否使用RegisterStartUpScript或RegisterClientScriptBlock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆