一聚教程网:一个值得你收藏的教程网站

热门教程

asp.net如何将后台c#数组传给前台js?

时间:2022-06-25 03:02:31 编辑:袖梨 来源:一聚教程网

第一步:定义cs数组

cs文件里后台程序中要有数组,这个数组要定义成公共的数组。

public string[] lat = null;

public string[] lng = null;

第二步:给cs数组赋值

cs数组的值一般都是从数据库中取到的,相信大家也都会,且后边的代码中也会有描写,这里就不做详细的解释。

第三步:将cs数组赋给前端的js数组

这个步骤是关键,我选用的方法就是<%=cs数组%>。这样模糊的说法也是百度得到的,赋值会用到循环,即会一个元素一个元素的赋值。

后台cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;
using System.Collections;

public partial class VideoSource : System.Web.UI.Page
{
    public string[] lat = null;//存放纬度值
    public string[] lng = null;//存放经度值
    public int lng_len = 0;//用于获得数组长度
    public int k = 0;//用于赋值循环
    protected void Page_Load(object sender, EventArgs e)
    {
       ArrayList lng_list = new ArrayList();
       ArrayList lat_list = new ArrayList();
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("App_Data/Database1.accdb"));
        con.Open();
        string sql = "select * from tb_videos";
        try
        {
            OleDbDataAdapter gh = new OleDbDataAdapter(sql, con);
            DataSet ds = new DataSet();
            gh.Fill(ds);
            con.Close();
            foreach (DataRow DR in ds.Tables[0].Rows)
            {
                lng_list.Add(DR[2].ToString());
                lat_list.Add(DR[3].ToString());
            }
        }
        catch
        {
            con.Dispose();
        }
        lng = (string[])lng_list.ToArray(typeof(string));
        lat = (string[])lat_list.ToArray(typeof(string));
        lng_len = lng_list.Count;
    }


aspx代码

上述代码即为我解决问题所用代码,均已试验通过。

热门栏目