简体   繁体   English

ASP.NET:基于Timer更改.aspx的背景图像

[英]ASP.NET: Change background-image of .aspx based on Timer

I created a program that will display the target and achievement of a group of Media and Subscription. 我创建了一个程序,该程序将显示一组媒体和订阅的目标和成就。 So I have a timer, where the data will change every 10 seconds. 所以我有一个计时器,数据每10秒更改一次。 My questions is, how can I also update or change the background-image of .aspx every 10 seconds?. 我的问题是,如何每10秒更新或更改.aspx的背景图像?

I have two background named: background_black.jpg and background_blue.jpg 我有两个背景名为:background_black.jpg和background_blue.jpg

Here's my CSS: 这是我的CSS:

   body{
    background-image: url('../Images/background_black.jpg');
    background-repeat:no-repeat;
    background-size: 100% auto;
    font-family: Eurostile;
    background-color:Black; 
}

and here's my source in .aspx: 这是我在.aspx中的来源:

<asp:UpdatePanel runat="server" id="UPThis2" UpdateMode="Conditional" >
        <ContentTemplate>
           <asp:Panel runat="server" ID="Panel1">
                 //Display first and the background-image is background_black.jpg
           </asp:Panel>

           <asp:Panel runat="server" ID="Panel2">
                 //After the first 10 seconds this Panel2 will displayed and also it will change also the background-image to background_blue.jpg
           </asp:Panel>
         </ContentTemplate>
</asp:UpdatePanel>

And lastly, here's my code for the Timer: 最后,这是我的计时器代码:

protected void Timer1_Tick(object sender, EventArgs e)
        {
            Timer1.Enabled = false;

            loadUserData();
            if (Global.__PUBLIC_indexcurrentdisply == _result2)
            {
                Panel2.Visible = true;
                Panel1.Visible = false;
            }
            else
            {
                Panel2.Visible = false;
                Panel1.Visible = true;
            }

            UPThis2.Update();
            Timer1.Enabled = true;
        }

Thank you for advance! 谢谢你的进步!

You can use simple javascript to achieve this 您可以使用简单的javascript来实现

On load, use settimeout method to change the background after 10 seconds 加载时,使用settimeout方法在10秒后更改背景

Settimeout method 设置超时方法

You can't update it from the Server Side. 您不能从服务器端更新它。 You have you use client side update or update callback. 您使用客户端更新或更新回调。 Because until the client requests, server does nothing. 因为在客户端请求之前,服务器什么都不做。 Better you use client side libraries. 更好地使用客户端库。 The best suited for your case is JavaScript. 最适合您的情况的是JavaScript。 Here would be a simple example of it - 这是一个简单的例子-

var _jsInterval = setInterval(function(){
     //do your task, change image etc.
}, 10 * 1000); //10s cooldown

add ClientIDMode1 attribute of use panels to be Static , so that you can refer them from client side then you simply get the items by - 将使用面板的ClientIDMode1属性添加为Static ,以便您可以从客户端引用它们,然后只需通过-

       <asp:Panel ClientIDMode="Static" runat="server" ID="Panel1">
             //Display first and the background-image is background_black.jpg
       </asp:Panel>

       <asp:Panel ClientIDMode="Static" runat="server" ID="Panel2">
             //After the first 10 seconds this Panel2 will displayed and also it will change also the background-image to background_blue.jpg
       </asp:Panel>

<script>
    var panel1 = document.getElementById('Panel1');
</script>

Therefore you would do something like - 因此,您将执行以下操作:

var _jsInterval = setInterval(function(){
     //do your task, change image etc.
     var panel1 = document.getElementById('Panel1');
     panel1.style.display = none; //etc
}, 10 * 1000); //10s cooldown

But again, it seems that you are changing it based on some value, so to get the value try ajax call..and update it on success method like this - (I used jQuery ajax) - 但是同样,看来您是根据某个值进行更改的,因此要获取该值,请尝试ajax调用..并在像这样的成功方法上进行更新-(我使用了jQuery ajax)-

 var _jsInterval = setInterval(function(){
     $.ajax({
          .....,
          success: function(data){
               //do your task, change image etc.
               var panel1 = document.getElementById('Panel1');
               panel1.style.display = none; //etc
          }
     });
}, 10 * 1000); //10s cooldown

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

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