简体   繁体   English

在 asp.net 3.5 中使用 RegisterClientScriptBlock/RegisterStartupScript

[英]Use of RegisterClientScriptBlock/RegisterStartupScript in asp.net 3.5

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.对此的任何想法都会很棒。

RegisterStartupScript is used to register and execute javascript functions once the page finished loading, this javascript code executes before the onLoad event of page is raised. RegisterStartupScript用于在页面加载完成后注册和执行 javascript 函数,此 javascript 代码在页面的 onLoad 事件引发之前执行。 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.您可以出于多种原因使用它,例如,您希望根据背后代码中的某个变量值执行任何特定的 javascript 函数。

On the other hand, RegisterClientScriptBlock is 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用于在页面顶部添加一个脚本块,类似地,它为您提供了一种在您的代码背后处理客户端代码和服务器代码的方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM