locked
Passing variables from C# to javascript RRS feed

  • Question

  • Hello everyone!

    I am trying to use the value of the variable 'user' in javascript, but the only thing I get is '<%= user %>' and not the real content of the variable. I have already declared 'user' as a public string , which gets its value from a textblock .....What I am doing wrong ?

    This is the code in C# when I push a button:

    string[] args = { "var user = '<%= user %>' ; document.getElementById('email').innerText = user;" };
                string foo = await browser.InvokeScriptAsync("eval", args);

    Thank you 

    Monday, March 17, 2014 4:51 PM

Answers

  • I asked because "'<%= user %>" is not valid syntax in Store applications.  However, it is valid in ASP.NET. It's not required to use substitution like that in Store apps. You should be able to do this:

    string[] args = { "document.getElementById('email').innerText = '" + user + "';" };
    string foo = await browser.InvokeScriptAsync("eval", args); 
    I condensed the code and put single quotes around the value of user since it's a string in javascript.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.


    Tuesday, March 18, 2014 1:00 PM
    Moderator
  • I've put together a more intensive code sample that passes a lot of data back and forth between C# and JavaScript in a Window Store app. I pass a JSON object as string to the browser and then process it to do things in JS and then return the results from JavaScript, pass them back as a string and parse them in C#. I mainly did this as a simple solution to doing complex graphics work without having to dive into directx: http://code.msdn.microsoft.com/Heat-Maps-in-Windows-Store-5e8982d9 I also have a blog post about this code sample here: http://blogs.msdn.com/b/bingdevcenter/archive/2014/03/11/heat-maps-in-windows-store-apps.aspx

    http://rbrundritt.wordpress.com

    Tuesday, March 18, 2014 1:48 PM

All replies

  • Is this a Store app or ASP.NET?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, March 17, 2014 7:07 PM
    Moderator
  • It' s a Store application. I would appreciate your help about the topic!

    Monday, March 17, 2014 10:58 PM
  • I asked because "'<%= user %>" is not valid syntax in Store applications.  However, it is valid in ASP.NET. It's not required to use substitution like that in Store apps. You should be able to do this:

    string[] args = { "document.getElementById('email').innerText = '" + user + "';" };
    string foo = await browser.InvokeScriptAsync("eval", args); 
    I condensed the code and put single quotes around the value of user since it's a string in javascript.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.


    Tuesday, March 18, 2014 1:00 PM
    Moderator
  • I've put together a more intensive code sample that passes a lot of data back and forth between C# and JavaScript in a Window Store app. I pass a JSON object as string to the browser and then process it to do things in JS and then return the results from JavaScript, pass them back as a string and parse them in C#. I mainly did this as a simple solution to doing complex graphics work without having to dive into directx: http://code.msdn.microsoft.com/Heat-Maps-in-Windows-Store-5e8982d9 I also have a blog post about this code sample here: http://blogs.msdn.com/b/bingdevcenter/archive/2014/03/11/heat-maps-in-windows-store-apps.aspx

    http://rbrundritt.wordpress.com

    Tuesday, March 18, 2014 1:48 PM
  • Thank you very much! It was very helpful to me!
    Tuesday, March 25, 2014 6:59 PM