如何在加载时注册一个Javascript

本文关键字:一个 Javascript 注册 加载 | 更新日期: 2023-09-27 18:03:01

我已经搜索了如何在文件。cs后面的代码中注册Javascript,但仍然没有理解它,我所尝试的没有触发Javascript。

如何在

中启动现有的javascript函数?
  protected void Page_Load(object sender, EventArgs e)
   {
   }

我试过了

Page.ClientScript.RegisterStartupScript(Type.GetType, "script", "return myFunction()");

但是它说它有无效参数?当我将红色下划线悬停时,会出现一些异常

谢谢你的帮助。

函数
function myFunction() {
            var combo = $find("<%= myClients.ClientID %>");
            //prevent second combo from closing
            cancelDropDownClosing = true;
            //holds the text of all checked items
            var text = "";
            //holds the values of all checked items
            var values = "";
            //get the collection of all items
            var items = combo.get_items();
            //enumerate all items
            for (var i = 0; i < items.get_count(); i++) {
                var item = items.getItem(i);
                //get the checkbox element of the current item
                var chk1 = $get(combo.get_id() + "_i" + i + "_chk1");
                if (chk1.checked) {
                    text += item.get_text() + ",";
                    values += item.get_value() + ",";
                }
            }
            //remove the last comma from the string
            text = removeLastComma(text);
            values = removeLastComma(values);
            if (text.length > 0) {
                //set the text of the combobox
                combo.set_text(text);
            }
            else {
                //all checkboxes are unchecked
                //so reset the controls
                combo.set_text("");
            }
        }

如何在加载时注册一个Javascript

GetType是一个方法。此外,您的脚本应该用脚本标签包装。试试这个:

Page.ClientScript.RegisterStartupScript(this.GetType(), 
                                        "script", "<script>myFunction();</script>");

试试这个:

Page.ClientScript.RegisterStartupScript(typeof(string), "script", "return myFunction()");

不能在函数外部使用返回语句。试试这个,希望它能帮助你提供函数"myFunction"在被调用之前存在于内存中。

Page.ClientScript.RegisterStartupScript(Type.GetType(), "script", "myFunction()", true);