将字符串列表传递给javascript,由asp代码在方法后面发送

pass list of string to javascript that is sent by asp code behind method

本文关键字:方法 代码 asp 列表 字符串 javascript      更新时间:2023-09-26

我试图在没有任何刷新的情况下在我的页面中显示在线火车。所以我想我必须使用javascript来做到这一点。所以我是javascript的初学者。所以让我解释一下我的问题。

我必须使用javascript:

执行这部分代码
  <%
            OnlineTrainList = TimeTableRepository.GetAll().ToList();
            foreach (var t in OnlineTrainList)
            {
                Response.Write("<div id='train-box' style='margin-left:"+(t.XTrainLocation-8)+"px;margin-top:"+t.YTrainLocation+"px;background:"+t.Train.TrainColor+"'>" +
                                   "<span>" +
                                        t.TrainId +
                                   "</span>" +
                               "</div>");
                         List<Sensor> PassedSensor = new List<Sensor>();
                         PassedSensor = SensorRepository.FindBy(i => i.CurrentTrainId == t.TrainId).ToList();
                         string color = TrainRepository.FindBy(i => i.Id == t.TrainId).First().TrainColor;
                foreach (Sensor sensor in PassedSensor)
                {
                    Response.Write("<div class='CurrentColor-Sensor' style='margin-left:" + (sensor.XLocation-1 ) + "px;margin-top:" + (sensor.YLocation+8) + "px;background:" + color + "'></div>");
                }
            }
             %>

此代码每次刷新并获得在线列车位置并显示在页面上,但我必须使用javascript

所以我创建了一个。asmx文件如下:
namespace ShirazRailWayWeb.Service
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
        public class SiteLocationService : System.Web.Services.WebService
    {
        public TimeTableRepository TimeTableRepository = new TimeTableRepository();
        public SensorRepository SensorRepository = new SensorRepository();
        public List<TimeTable> OnlineTrainList = new List<TimeTable>();
        public TrainRepository TrainRepository = new TrainRepository();
        [WebMethod]
        public string myfunc()
        {
        }
    }
}

我的问题是如何将值列表传递给javascript。我应该在َُ**asmx**文件中加载我的数据并在我的页面中调用此函数。但我不知道如何将数组在线列车列表传递给我的页面。

这是我的存储库,生成的数据。但这个数据是一个列表,我不知道我怎么能把这个数据传递到我的页面!!

 public TimeTableRepository TimeTableRepository = new TimeTableRepository();
            public SensorRepository SensorRepository = new SensorRepository();
            public List<TimeTable> OnlineTrainList = new List<TimeTable>();
            public TrainRepository TrainRepository = new TrainRepository();
任何想法。

您可以使用Asp。Net UpdatePanel为您处理部分页面渲染。

为了让你的UpdatePanel的内容按照你的请求每秒钟更新一次,你需要使用javascript来调用自动渲染的Asp。. Net回发函数,以更新面板的id作为参数。

下面是一个完整的服务器端时钟示例:

在你的aspx页面的html部分添加这个block:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" OnLoad="UpdatePanel1_Load">
    <ContentTemplate>
        <label id="Label_For_Server_Time" runat="server"></label>
    </ContentTemplate>
</asp:UpdatePanel>

在这里你可以看到一个标签运行在服务器上,所以我们可以从后面的代码访问它;

在你的aspx页头,添加这个:

<script type="text/javascript">
    $(function () {
        setInterval("__doPostBack('<%=UpdatePanel1.ClientID%>', '');", 1000);
    });
</script>

这段代码负责每秒更新一次updatepanel,或者说是每1000毫秒更新一次。
函数__doPostBack()是一个asp.net生成的函数,通过调用setInterval,我们使更新面板每秒钟发布一次。
这里的$(function (){});包装器只是为了防止在DOM完全加载之前执行代码。你用jQuery标记了你的问题,所以我想你没问题。

在您的代码后面,您需要添加updatepanel的加载处理程序,它在面板发布时运行:

protected void UpdatePanel1_Load(object sender, EventArgs e)
{
    Label_For_Server_Time.InnerText = DateTime.Now.ToString();
}