简体   繁体   中英

Javascript null variable in asp.net inline

In my asp.net page FieldId is the page-class level variable in code behind

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

 <script language="javascript" type="text/javascript">

    var searchFieldId = '<%= FieldId %>';
</script>

Whenever I have a FieldId value null the page renders like below

<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#:

var searchFieldId = <%= (FieldId.HasValue ? FieldId.Value.ToString() : "null") %>;

or VB:

var searchFieldId = <%= If(FieldId.HasValue, FieldId.Value.ToString(), "null") %>;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM