asp.net - access a backend variable in front end using javascript - Stack Overflow

I had Public declared Dictonary in code behind as :Public dics As New Dictionary(Of String, String()) F

I had Public declared Dictonary in code behind as :

 Public dics As New Dictionary(Of String, String()) From { _
{"picture", New String() {".jpeg", ".jpg", ".png", ".bmp", ".gif", ".tif"}}, _
{"document", New String() {".doc", ".docx", ".txt", ".htm", ".html", ".xml", ".xaml", ".css"}}, _
{"excel", New String() {".xls", ".xlsx", ".xlt", ".xla"}}, _
{"pdf", New String() {".pdf"}}, _
{"zip", New String() {".7z", ".APK", ".BAT", ".rar", ".dll", ".jar", ".zip"}}, _
{"ppt", New String() {".ppt", ".pos", ".pps"}}}

Edit :

if i do like this

function myFunction() {
       var dic = "<%= dics %>";
       var array_keys = new Array();
       var array_values = new Array();
       for (var key in dic) {
           alert(key);
        }
     }

will show alerts as

How can i access this Dictonary in javascript to do some operations

I had Public declared Dictonary in code behind as :

 Public dics As New Dictionary(Of String, String()) From { _
{"picture", New String() {".jpeg", ".jpg", ".png", ".bmp", ".gif", ".tif"}}, _
{"document", New String() {".doc", ".docx", ".txt", ".htm", ".html", ".xml", ".xaml", ".css"}}, _
{"excel", New String() {".xls", ".xlsx", ".xlt", ".xla"}}, _
{"pdf", New String() {".pdf"}}, _
{"zip", New String() {".7z", ".APK", ".BAT", ".rar", ".dll", ".jar", ".zip"}}, _
{"ppt", New String() {".ppt", ".pos", ".pps"}}}

Edit :

if i do like this

function myFunction() {
       var dic = "<%= dics %>";
       var array_keys = new Array();
       var array_values = new Array();
       for (var key in dic) {
           alert(key);
        }
     }

will show alerts as

How can i access this Dictonary in javascript to do some operations

Share Improve this question edited Sep 30, 2014 at 12:52 asked Sep 16, 2014 at 6:25 user3972104user3972104 3
  • check if this thread gives you idea to try – har07 Commented Sep 16, 2014 at 6:47
  • If the values stored in dictionary is static then you can try by declaring the js variables containing these values. – Priya Commented Sep 16, 2014 at 7:04
  • @ Priya : here the values are static that wont change dynamically – user3972104 Commented Sep 16, 2014 at 7:09
Add a ment  | 

2 Answers 2

Reset to default 3 +25

For now it looks like you need to serialize dictionary into javascript object and then paste it in your JavaScript. You could use any library for serialization. E.g. Newtosoft.Json. Like this:

function myFunction() {
   var dic = <%= Newtonsoft.Json.JsonConvert.SerializeObject(dics) %>;
   var array_keys = new Array();
   var array_values = new Array();
   for (var key in dic) {
       alert(key);
    }
 }

Note that I removed quotes.

But I suggest you to get rid of this approach and not mix JavaScript and ASP.Net code. So in my vision you should load this dictionary via AJAX or if it's not possible place ASP.Net in some other place. E.g.:

View:

<input type="hidden" id="dictionaryInput" value="<%=Newtonsoft.Json.JsonConvert.SerializeObject(dics)%> />

JavaScript:

function myFunction() {
   var dicInput = document.getElementById('dictionaryInput');
   var dic = JSON.parse(dicInput.value);
   var array_keys = new Array();
   var array_values = new Array();
   for (var key in dic) {
       alert(key);
    }
 }

Hope it will help.

You can create a property(say DictionaryConv) in your code behind, and in page load set that property value.

Dim jsz As New System.Web.Script.Serialization.JavaScriptSerializer
        DictionaryConv = jsz.Serialize(dics)

In javascript you use this function.

function myFunction() {
            var dic = <%= DictionaryConv%>;
            var array_keys = new Array();
            var array_values = new Array();
            for (var key in dic) {
                alert(key);
            }
        }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745242424a4618257.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信