gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:28:30 25 4
gpt4 key购买 nike

我已经搜索了如何在文件 .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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com