asp.net - 在asp.net后面的代码中创建javaScript变量

标签 asp.net

如何在服务器端(后端)注册Javascript变量并在客户端(Javascript文件)访问它,而没有隐藏字段,文字等?

最佳答案

您可以从页面的ClientScriptManager中使用RegisterClientScriptBlock -Function。

Page.ClientScript.RegisterClientScriptBlock(Page.GetType, "initMyClientVariable", "var myClientVariable=null;", True)

编辑:根据您的新信息,要注册客户端阵列,请使用ClientScriptManager的RegisterArrayDeclaration方法。

VB.Net示例:
Dim myArrayValue As String = """1"", ""2"", ""text"""
Page.ClientScript.RegisterArrayDeclaration("myClientArray", myArrayValue)

根据我的评论中的新信息,您需要从外部js文件访问该变量:您应该将js数组作为参数传递给js文件中的函数。例如:
callFunctionInJsFile(checkBoxes);

关于asp.net - 在asp.net后面的代码中创建javaScript变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4398338/

相关文章:

asp.net - 标签在 FooterTemplate/Panel/SecurePanel/Div 中使用时不起作用

c# - ASP.NET 成员(member)资格提供程序无法在 LINQ 查询中返回用户角色

c# - 将 SqlDataSource 移动到类文件 ASP.NET

c# - 将我的 Web 应用程序从 .Net 3.5 升级到 .Net4.0 后,出现安全透明规则失败

c# - 使用不记名 token 身份验证时设置 asp.net CORE 2 身份验证 cookie

asp.net - 如何在 GridView Eval 中拆分逗号分隔的数据项字段?

c# - 用户代理导致 MVC DisplayFor ArgumentException : Illegal characters in path

javascript - 使用 ASP.net 控件和 Telerik 控件禁用表

c# - BasicHttpBinding 不使用默认配置

c# - 将样式表应用于 gridview 模板字段