如何在一个功能EXCUTE多个ClientScript.RegisterStartupScript()? [英] how to Excute more than one ClientScript.RegisterStartupScript() in one function?

查看:132
本文介绍了如何在一个功能EXCUTE多个ClientScript.RegisterStartupScript()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要执行从服务器端JavaScript函数,所以我说下面一行:

I want to execute javascript function from server side, so i added following line:

 ClientScript.RegisterStartupScript(Page.GetType(), "@msg", "ValidateRequiredFields('" + txtRequiredFieldValidation.ClientID + "');", true);

它工作正常。但我想上面的语句之后立即执行相同的JavaScript函数。
所以我加了以下几行:

it works fine. but i want to execute same JavaScript function immediately after above statement. so i added following lines :

ClientScript.RegisterStartupScript(Page.GetType(), "@msg", "ValidateRequiredFields('" + txtEmailValidation.ClientID + "');", true);
        ClientScript.RegisterStartupScript(Page.GetType(), "@msg", "ValidateRequiredFields('" + txtOnlyNumbersValidations.ClientID + "');", true);

但我注意到,只有第一个startupscript执行。为什么?我怎么能在一个函数执行多个startupscript?

but i noticed that only first one startupscript executes.. why? how can i execute multiple startupscript in one function?

推荐答案

根据 MSDN

用的RegisterStartupScript方法添加的脚本块在页面加载完毕,但页面的onload事件引发之前执行。脚本块不保证>是在他们注册的顺序输出。如果脚本块的顺序是>重要的是,使用StringBuilder对象在一个字符串来收集脚本在一起,>然后注册他们都在一个单一的客户端脚本块。

The script block added by the RegisterStartupScript method executes when the page finishes loading but before the page's OnLoad event is raised. The script blocks are not guaranteed >to be output in the order they are registered. If the order of the script blocks is >important, use a StringBuilder object to gather the scripts together in a single string, and >then register them all in a single client script block.

所以,把共同呼吁应为你工作。

So putting the call together should work for you

"ValidateRequiredFields('" + txtEmailValidation.ClientID + "');
 ValidateRequiredFields('" + txtOnlyNumbersValidations.ClientID + "');"

这篇关于如何在一个功能EXCUTE多个ClientScript.RegisterStartupScript()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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