javascript 从ASP向js传递两个数组

wmomyfyw  于 2023-03-06  发布在  Java
关注(0)|答案(1)|浏览(112)

我工作的看板清单包含正在进行和待办事项清单与asp和sql到现在为止,我可以通过第一个列表成功,但不能添加第二个列表“ongo”我的代码如下

<script>
    document.addEventListener('DOMContentLoaded', function () {
        $.ajax({
            type: "POST",
            url: "index.aspx/GetEvents",
            tasks: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json"
        }).done(function (tasks) {
            var DuT = JSON.parse(tasks.d);
            var OpT = JSON.parse(Otasks.d);
            $('#todo-lists-basic-demo').lobiList({
                lists: [
                    {
                        id: 'todo',
                        title: 'TODO',
                        defaultStyle: 'lobilist-info',
                        items: DuT,
                    },
                    {
                        id: 'doing',
                        title: 'DOING',
                        defaultStyle: 'lobilist-default',
                        items: OpT,
                    },
                ]
            });
        });
    });
</script>

        public class Task
    {
        public string title, description, dueDate;
    }
    public class Ongo
    {
        public string title, description, dueDate;
    }
    [WebMethod]
    public static string GetEvents()
    {
        var tasks = new List<Task>();
        var Otasks = new List<Ongo>();
        String sqlconn = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(sqlconn))
        {
            using (SqlCommand SqC = new SqlCommand("Select titl,dscr, due FROM KNtsk WHERE opnd = 1 Order by due", con))
            {
                con.Open();
                using (SqlDataReader sdr = SqC.ExecuteReader())
                {
                    if (sdr.HasRows)
                    {
                        while (sdr.Read())
                        {
                            tasks.Add(new Task()
                            {
                                dueDate = DateTime.Parse(sdr["due"].ToString()).ToString("d"),
                                title = sdr["titl"].ToString(),
                                description = sdr["dscr"].ToString()
                            });
                        }
                    }
                }
                con.Close();
            }
            using (SqlCommand SqC = new SqlCommand("Select titl,dscr, due FROM KNtsk WHERE opnd = 0 Order by due", con))
            {
                con.Open();
                using (SqlDataReader sdr = SqC.ExecuteReader())
                {
                    if (sdr.HasRows)
                    {
                        while (sdr.Read())
                        {
                            Otasks.Add(new Ongo()
                            {
                                dueDate = DateTime.Parse(sdr["due"].ToString()).ToString("d"),
                                title = sdr["titl"].ToString(),
                                description = sdr["dscr"].ToString()
                            });
                        }
                    }
                }
                con.Close();
            }
            var stz = new JavaScriptSerializer();
            return stz.Serialize(tasks);
        }
    }

当我删除“var OpT = JSON.parse(Otasks.d)”时,它可以正常工作;“从脚本中,我知道在传递这两个数组时做了一些错误的事情

nqwrtyyt

nqwrtyyt1#

我觉得“有点傻”

<script>
    $(function () {
        var DuT = JSON.parse('<%= this.Mytasks %>');
        var OpT = JSON.parse('<%= this.MyOtasks %>');
        $('#todo-lists-basic-demo').lobiList({
            lists: [
                {
                    id: 'todo',
                    title: 'TODO',
                    defaultStyle: 'lobilist-info',
                    items: DuT,
                },
                {
                    id: 'doing',
                    title: 'DOING',
                    defaultStyle: 'lobilist-default',
                    items: OpT,
                },
            ]
        });
    });
</script>

和代码隐藏

protected string MyOtasks
    {
        get
        {
            var tasks = new List<Task>();
            String sqlconn = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(sqlconn))
            {
                using (SqlCommand SqC = new SqlCommand("Select titl,dscr, due FROM KNtsk WHERE opnd = 0 Order by due", con))
                {
                    con.Open();
                    using (SqlDataReader sdr = SqC.ExecuteReader())
                    {
                        if (sdr.HasRows)
                        {
                            while (sdr.Read())
                            {
                                tasks.Add(new Task()
                                {
                                    dueDate = DateTime.Parse(sdr["due"].ToString()).ToString("d"),
                                    title = sdr["titl"].ToString(),
                                    description = sdr["dscr"].ToString()
                                });
                            }
                        }
                    }
                    con.Close();
                }
            }
            var stz = new JavaScriptSerializer();
            return stz.Serialize(tasks);
        }
    }

相关问题