更新面板没有在asp.net工作 [英] Update panel doesn't work in asp.net

查看:121
本文介绍了更新面板没有在asp.net工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,以显示我的网页在线列车没有任何刷新。所以我觉得我必须使用的UpdatePanel 。我的面板应每秒更新一次。

I am trying to show online train in my page without any refresh. So I think I have to use updatepanel. My panel should be updated every second.

让我解释一下code。

Let explain my code.

我把code的这一部分在我的网页:

I put this part of code in my page:

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" OnLoad="UpdatePanel1_Load">
    <ContentTemplate>
    </ContentTemplate>
</asp:UpdatePanel>

所以我把间隔函数刷​​新我的板是这样,

So I put an interval function to refresh my panel like this,

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

在我的code后面我把所有提取的数据,

In my code behind I put all fetched data,

protected void UpdatePanel1_Load(object sender, EventArgs e)
{
    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>");
        }
    }
}

所以我的问题是,面板不会刷新。而我的面板 UpdatePanel1_Load 只需拨打一次。

推荐答案

所以你的code是:

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>");
    }
}

第一件事,我必须将注意力集中到 ID 分配给所有的div,它必须是唯一的,绝不相同。你可以使用一个增量的变量并将它添加到您的div的ID例如: DIV0,DIV1 ...等

first thing i must bring your attention to the id that you assign to all those divs, it must be unique and never the same. you could use an increment variable and append it to your div's id eg: div0, div1... etc

现在让我们来看看在循环中的第一行。它主要有,是一个&LT;跨度&GT; 嵌套在&LT; D​​IV&GT; 和包含文本的某些属性

now lets have a look at the first row inside the loop. what it basically has, is a <span> nested inside a <div> and containing some attributes with text.

处理元素的Asp.Net方法是面向对象的,而不是仅仅HTML字符串:

protected void UpdatePanel1_Load(object sender, EventArgs e)
{
    //clear the update panel
    UpdatePanel1.ContentTemplateContainer.Controls.Clear();
    //var to generate unique div id's 
    int divIDIncrement = 0;
    foreach (var t in OnlineTrainList)
    {
        //increment the id generator 
        divIDIncrement++;
        // create a a DIV element
        HtmlGenericControl _tempDIV = new HtmlGenericControl("div");
        //set the attributes for the div
        _tempDIV.ID = "train-box" + divIDIncrement.ToString();
        _tempDIV.Style["margin-left"] = (t.XTrainLocation - 8).ToString() + "px";
        _tempDIV.Style["margin-top"] = t.YTrainLocation.ToString() + "px";
        _tempDIV.Style["background"] = t.Train.TrainColor.ToString();
        //create the inner span
        HtmlGenericControl _tempSPAN = new HtmlGenericControl("span");
        //set the span's innerText
        _tempSPAN.InnerText = t.TrainId.ToString();
        //add the span into the Div
        _tempDIV.Controls.Add(_tempSPAN);
        //add the Div into your UpdatePanel
        UpdatePanel1.ContentTemplateContainer.Controls.Add(_tempDIV);


        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)
        {
            // create another div for the sub stuff
            HtmlGenericControl _tempSubDIV = new HtmlGenericControl("div");
            _tempSubDIV.Attributes["class"] = "CurrentColor-Sensor";
            _tempSubDIV.Style["margin-left"] = (sensor.XLocation - 1).ToString() + "px";
            _tempSubDIV.Style["margin-top"] = (sensor.YLocation + 8).ToString() + "px";
            _tempSubDIV.Style["background"] = color.ToString();
            //add the sub stuff to the update panel
            UpdatePanel1.ContentTemplateContainer.Controls.Add(_tempSubDIV);
        }
    } 
}

这篇关于更新面板没有在asp.net工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆