简体   繁体   English

更新面板在asp.net中不起作用

[英]Update panel doesn't work in asp.net

I am trying to show online train in my page without any refresh. 我正在尝试在页面上显示在线火车,而无需进行任何刷新。 So I think I have to use updatepanel . 所以我认为我必须使用updatepanel My panel should be updated every second. 我的面板应该每秒更新一次。

Let explain my 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);
    });

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

So my problem is the panel doesn't refresh .And my panel UpdatePanel1_Load just call one time. 所以我的问题是面板不刷新。面板UpdatePanel1_Load只调用一次。

So your code is: 所以你的代码是:

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

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. 首先,我必须引起您注意分配给所有这些div的id ,它必须是唯一的且永不相同。 you could use an increment variable and append it to your div's id eg: div0, div1... etc 您可以使用一个增量变量并将其附加到您的div的ID上,例如: div0, div1... etc

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. 它基本上具有的是<span>嵌套在<div>并包含一些带有文本的属性。

the Asp.Net way of handling elements would be object oriented instead of just html string: 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);
        }
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM