简体   繁体   English

如何将服务器端类型传递给asp.net Webform以使用JavaScript

[英]how to pass a serverside Type to asp.net webform for javascript consumption

I'm currently trying to pass a known Type (List) to a asp.net webform that needs to be picked up by javascript on the pageload. 我目前正在尝试将已知的类型(列表)传递给需要由页面加载中的javascript拾取的asp​​.net网络表单。

the mock data I am creating looks like this : 我正在创建的模拟数据如下所示:

   protected List<MapCoords> createCoordinateList()
    {
       List<MapCoords> latlng = new List<MapCoords>();
       MapCoords m = new MapCoords();
       m.xCoord = 34.241182;
       m.yCoord = -77.946839;
       latlng.Add(m);
       m.xCoord = 34.242176;
       m.yCoord = -77.94538;
       latlng.Add(m);
       return latlng
     }

Where I am stuck (maybe just late in the day) is that in the past I have made an ajax call and returned the object on success of the result back to the page. 我受困的地方(也许只是一天中的很晚)是,过去我进行了ajax调用,并在结果成功后将对象返回给页面。 However in this scenario this mock data is being generated by a sever side event so ajax is not used to invoke or call the method. 但是,在这种情况下,此模拟数据是由服务器端事件生成的,因此不使用ajax来调用或调用该方法。 Since the object has multiple properties (x,y) I can't just set the values to a hidden field which is why I think this needs to be passed as an object. 由于对象具有多个属性(x,y),所以我不能仅将值设置为隐藏字段,这就是为什么我认为需要将其作为对象传递的原因。 If push comes to shove I could use ajax to invoke the method but this would almost be a double call for what I am trying to do so I would like to avoid this is possible 如果推推推,我可以使用ajax来调用该方法,但这几乎是对我尝试执行的操作的双重调用,因此我想避免这种情况的发生

So the question is how can I take this list containing the coordinates and make it accessible to javascript in the client? 因此,问题是如何获取包含坐标的列表并使客户端中的javascript可以访问它? This type could become more robust (ie more properties so I need to ensure that the properties retain their association x to y ) 这种类型可能会变得更健壮(即,具有更多属性,因此我需要确保这些属性保留它们与x到y的关联)

cheers, 干杯,

You can pass array of class using web method both in aspx page or .asmx webservice. 您可以在aspx页面或.asmx网络服务中使用网络方法传递类数组。 ScriptService attribute will convert your class array to json object. ScriptService属性会将您的类数组转换为json对象。 This simple example to understand how you can do it, jQuery Ajax with asp.net 这个简单的例子,了解如何实现, 带有asp.net的jQuery Ajax

[ScriptService]
public class YourClass : Page
{
    [WebMethod]   
    public static []MapCoords createCoordinateList()
    {
        //Your code
        return arrOfMapCoords;   
    }
}

Created a little example for you: 为您创建了一个小示例:

Server code: 服务器代码:

public class MapCoords
{
    public double x { get; set; }
    public double y { get; set; }
}
public partial class _Default : System.Web.UI.Page
{
    [WebMethod]
    public static List<MapCoords> createCoordinateList()
    {
        List<MapCoords> latlng = new List<MapCoords>();
        MapCoords m = new MapCoords();
        m.x = 2.00;
        m.y = 3.1512;
        latlng.Add(m);

        m = new MapCoords();
        m.x = 3.00;
        m.y = 4.1512;
        latlng.Add(m);
        return latlng;
    }
}

Client Code: 客户代码:

<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        $.ajax({
            type: "POST",
            url: "Default.aspx/createCoordinateList",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                if (msg != null) {
                    for (i = 0; i <= msg.d.length; i++) {
                        alert(msg.d[i].x + " " + msg.d[i].y);   
                    }
                }
            }
        });
    });
</script>

This uses jQuery and jSon. 这使用jQuery和jSon。 Don't forget to include the jQuery library. 不要忘记包括jQuery库。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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