javascript 在 asp.net 3.5 中使用 RegisterClientScriptBlock/RegisterStartupScript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6951451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:27:59  来源:igfitidea点击:

Use of RegisterClientScriptBlock/RegisterStartupScript in asp.net 3.5

c#javascriptasp.net

提问by Kumee

I'm not able to understand what is the use of it.RegisterClientScriptBlock/RegisterStartupScript.

我无法理解它的用途是什么。RegisterClientScriptBlock/RegisterStartupScript。

When we can directly write the JavaScript code in.js file then call it on the button

当我们可以直接在.js文件中编写JavaScript代码然后在按钮上调用

ex: 2

例如:2

<script>
    function ReqField1Validator() { 
        if (document.forms[0].txtField1.value == '') {
            alert('TextBox cannot be empty') 
            return false
         } 
         return true 
     } 
</script>

btnPostback.Attributes.Add("onclick","return ReqField1Validator()");

What will be the use of RegisterClientScriptBlock/RegisterStartupScript?

RegisterClientScriptBlock/RegisterStartupScript 的用途是什么?

protected void btnPostback_Click(object sender, EventArgs e)  {
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.Append(@"<script language='javascript'>");
    sb.Append(@"var lbl = document.getElementById('lblDisplayDate');");
    sb.Append(@"lbl.style.color='red';");
    sb.Append(@"</script>");

    if (!ClientScript.IsStartupScriptRegistered("JSScript")){
        ClientScript.RegisterStartupScript(this.GetType(), "JSScript", sb.ToString());
    }
}

Read few articles but they were not clear.

看了几篇文章,但都不是很清楚。

Any thoughts on this would be great.

对此的任何想法都会很棒。

采纳答案by Waqas

RegisterStartupScriptis used to register and execute javascript functions once the page finished loading, this javascript code executes before the onLoad event of page is raised. You can use it for multiple reasons, for example, you want to execute any particular javascript function depending on some variable value in your code behind.

RegisterStartupScript用于在页面加载完成后注册和执行 javascript 函数,此 javascript 代码在页面的 onLoad 事件引发之前执行。您可以出于多种原因使用它,例如,您想根据背后代码中的某个变量值执行任何特定的 javascript 函数。

On the other hand, RegisterClientScriptBlockis used to add a script block on the top of your page, similarly it provides you a way to work with both client side code and server code in your code behind.

另一方面,RegisterClientScriptBlock用于在页面顶部添加脚本块,类似地,它为您提供了一种在后面的代码中处理客户端代码和服务器代码的方法。

回答by mario.tco

In time, you might come across situations in which you will need to call a function in JavaScript depending on some variants in your code behind.

随着时间的推移,您可能会遇到需要根据背后代码中的某些变体调用 JavaScript 中的函数的情况。

A very common example would be to handle exception messages and then display them in a fancy way using JavaScript.

一个非常常见的例子是处理异常消息,然后使用 JavaScript 以一种奇特的方式显示它们。

You might want to catch an exception like this:

您可能想要捕获这样的异常:

public void TrySomethingAwesome(){
    try
    {
        //try to do something awesome
    }
    catch (Exception ex)
    {   
        ScriptManager.RegisterStartupScript(Page, typeof(Page), "showError",
            string.Format("ShowError({0});", "Oops! Something went wrong :("), true);
    }
}

And that way your JavaScript can take over and display the message in a fancy way :D

这样你的 JavaScript 就可以接管并以一种奇特的方式显示消息 :D