简体   繁体   中英

Accessing C# Variables in JavaScript

I am Developing Windows 8 application in which i have to access variables of C# class in java script function, But Unfortunately i don not know how to do this: My C# class code is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pakistan_Tour
{
   public class UniversalValues
{
   public static double xC;
   public static double yC;

public  static  int selectedcity;

     public int retcity() 
    {
        return selectedcity;

    }
    public void setcity(int val)
    {
         selectedcity=val;
    }
    public void setxy(double x, double y)
    {
        xC = x;
        yC = y;

    }
    public double getx()
    {
        return xC;
    }
    public double gety()
    {
        return yC;
    }


 }
}

I have to access the value of variables: xC and yc in java script I am doing like:

 function initialize() {

        var x = "<%=xC%>";

        var y = "<%=yC%>";
}

but is not working, Kindly help me with this, Thanks in advance.

Here is a walkthrough about calling C# from JS. Also, take a look at this question .

Try passing c# vaiables as parameters to your JS function

<script type="text/javascript">
    generateMarkersForSideBar(<%=this.LatLangStringForMap%>);
</script>

and in cs code

string _LatLangStringForMap;
public string LatLangStringForMap
{
  get { return _LatLangStringForMap; }
  set { _LatLangStringForMap = value; }
}

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