[英]Javascript null variable in asp.net inline
In my asp.net page FieldId
is the page-class level variable in code behind 在我的asp.net页面中,
FieldId
是页面代码级别的页面类变量
protected int? FieldId= null;
Then I try to set the same value to the Javascript variable, so whenever page gets loaded I can access the same in Javascript and do some stuff 然后,我尝试为Javascript变量设置相同的值,因此每当页面加载时,我都可以使用Javascript访问相同的内容并做一些事情
<script language="javascript" type="text/javascript">
var searchFieldId = '<%= FieldId %>';
</script>
Whenever I have a FieldId
value null the page renders like below 每当我的
FieldId
值为null时 ,页面就会如下所示
<script language="javascript" type="text/javascript">
var searchFieldId = ; // but here i need null
</script>
Since the variable initialization has the improper syntax i get syntax error. 由于变量初始化的语法不正确,因此出现语法错误。
How do we write null here. 我们如何在这里写空值。
You need a workaround like the following in C#: 您需要在C#中采用以下解决方法:
var searchFieldId = <%= (FieldId.HasValue ? FieldId.Value.ToString() : "null") %>;
or VB: 或VB:
var searchFieldId = <%= If(FieldId.HasValue, FieldId.Value.ToString(), "null") %>;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.