代码之家  ›  专栏  ›  技术社区  ›  Brad8118

从aspx.cs页获取数据到aspx页

  •  0
  • Brad8118  · 技术社区  · 14 年前

    所以我使用了一个jquery插件,它允许我通过拖放来更改列表中事物的顺序。 因此,我的目标是能够获取我的对象列表(alertinfo),并在javascript函数中使用它。 我可以在测试项目中使用JSONWebService调用将数据传递给页面。 但是我们现在没有WebService页面,所以我试图从aspx.cs页面中获取它,但它没有工作。

    ///ASPX页:

    $.ajax({
       type: "POST",
       url: "~/Alerts/GetAlerts",
       data: "{}",
       contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            var data = eval("(" + msg.d + ")");
            jQuery.each(data, function (rec) {
            AlertList[AlertList.length] = new objAlert(this.id, this.title, this.details,               JSONDateSerializationFix(this.startdate), JSONDateSerializationFix(this.enddate));
            UpdateDisplayList();
         })
         },
         error: function (msg) {
            alert("BRAD" + msg);
         }
    

    问题是“url/alerts/getalerts”中的alerts页面是alerts.aspx.cs。我不知道是否可以使用这个Ajax命令调用aspx.cs页面中的方法。
    //代码隐藏页aspx.cs

    [WebMethod]
     //[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetAlerts()
    {
      List<AlertInfo> list = AlertInfo.GetTestAlerts();
      return new JavaScriptSerializer().Serialize(list);
    }
    
    public List<AlertInfo> GetAlertsList()
    {
      List<AlertInfo> list = AlertInfo.GetTestAlerts();
      return list; ;
    }
    

    所以我希望可以将数据加载到ASP控件(datalist)中,然后获取数据

    //代码隐藏页

    protected void Page_Load(object sender, EventArgs e)
    {
        dataListAlertList.DataSource = GetAlertsList();
        dataListAlertList.DataBind();
    }
    public static List<AlertInfo> GetTestAlerts()
    {
                List<AlertInfo> list = new List<AlertInfo>();
                list.Add(new AlertInfo("0", "Alert 1 Title", "Alert 1 Detail", "10/10/2010", "10/10/2011"));
                list.Add(new AlertInfo("1", "Alert 2 Title", "Alert 2 Detail", "10/10/2010", "10/10/2011"));
                return list;
    

    }

    //ASPX页

    $(document).ready(function () {
        var a1 = $("#dataListAlertList").val();
        // do fun stuff now.
    }
    

    但我一直没有定义……

    1 回复  |  直到 14 年前
        1
  •  0
  •   Brad8118    14 年前

    不需要预加载。最后使用了数据列表,然后更改了它的格式。在document ready函数中,我抓取了div并应用了需要设置的属性。