c# - 如何在加载时注册 Javascript

标签 c# asp.net

我已经搜索了如何在文件 .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("");
            }
        }

最佳答案

GetType 是一种方法。此外,您的脚本应该包含在脚本标签中。试试这个:

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

关于c# - 如何在加载时注册 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6748913/

相关文章:

c# - 无法添加对 Outlook 2016 (Office 365) Interop (16.0.0.0) 的引用

c# - 在 C# 中配置 BrowserMobProxy

c# - Asp.net MVC 4.5 使用 EditorFor、按钮单击将对象添加到 ViewModel 中的列表

asp.net - Asp.net用户角色管理: Where to Begin

asp.net - MSBuild:自动收集数据库迁移脚本?

c# - 在 asp.net 中使用 webbrowser 将 html 页面转换为图像需要太多时间来生成重 html 页面的图像

c# - 如何检查 ASP.NET 页面上的文本区域是否已被编辑?

c# - 如何使用 ASP.NET MVC Core 初始设置角色和用户(如站点管理员)?

C# 编译数值表达式

c# - 为 EF 5.0 模型提供连接字符串