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

时间:2014-06-20 10:03:38

标签: c# asp.net updatepanel

我试图在我的页面中显示在线列车而不进行任何刷新。所以我想我必须使用updatepanel。我的面板应该每秒更新一次。

让我解释一下我的代码。

我把这部分代码放在我的页面中:

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

所以我把一个区间函数像这样刷新我的面板,

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

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

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只调用一次。

1 个答案:

答案 0 :(得分:1)

所以你的代码是:

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

首先,我必须将注意力集中在您分配给所有div的id上,它必须是唯一的,而且永远不会相同。您可以使用增量变量并将其附加到div的ID中,例如:div0, div1... etc

现在让我们看看循环中的第一行。它基本上有一个<span>嵌套在<div>内并包含一些带文字的属性。

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);
        }
    } 
}
相关问题