简体   繁体   English

ASP.NET网页-使用脚本显示由预定义的日期和时间指示的图像/文本

[英]ASP.NET Webpage - Displaying image/text dictated by predefined date and time using scripts

My goal is to display a string or image on a ASP.NET webpage only during a predefined date and time (Monday through Friday, 9:00AM-5:00PM). 我的目标是仅在预定义的日期和时间(星期一至星期五,9:00 AM-5:00PM)内在ASP.NET网页上显示字符串或图像。 Keep in mind, I will be relying on fixed server time, PST. 请记住,我将依靠固定服务器时间PST。

I am not sure whether to use jQuery, JavaScript, C# or PHP for the script. 我不确定该脚本是否使用jQuery,JavaScript,C#或PHP。 Here is what I have come up with so far in JavaScript. 到目前为止,这是我在JavaScript中提出的。 Am I on the right path? 我在正确的道路上吗?

<script type="text/javascript" language="javascript">
function GetDateTime()
{
var d = new Date();
var day = d.getDay(); //returns 0-6 corresponding to Sunday through Saturday
var hour = d.getHours(); //returns 0-23 corresponding to hours 
}   
</script>

Now I need help manipulating the variables 'day' and 'hour'. 现在,我需要帮助操作变量“ day”和“ hour”。 I came up with a generic algorithm to specify Monday through Friday, 9:00AM - 5:00PM as follows: 我想出了一种通用算法来指定星期一至星期五的9:00 AM-5:00 PM,如下所示:

if ((day>0 && day<6) && (hour>8 && hour<18) ) //days 1 through 5 (M-F) and (hours 9:00-17:00)
{ alert("testing") 
//This is for testing purposes. I will eventually insert code here to display string or image perhaps? 
}

Assuming the date and time are properly obtained and the if statement algorithm is correct, how do I implement the Javascript portion and "tie the code together" with my ASP.NET page to display a string or image based on those parameters? 假设正确获得了日期和时间,并且if语句算法正确,那么如何实现Javascript部分并将代码与ASP.NET页面“绑定在一起”,以基于这些参数显示字符串或图像?

This is my first time posting on Stackoverflow. 这是我第一次在Stackoverflow上发帖。 I hope I am addressing this question properly and clearly enogh. 我希望我能正确,明确地解决这个问题。 Thanks in advance for any help. 在此先感谢您的帮助。


Edit: I gave up on JavaScript for now. 编辑:我现在放弃了JavaScript。 C# seems to me like the proper way of doing this. C#在我看来似乎是执行此操作的正确方法。 Thanks to codingbiz 's resonse, I was able to implement the script as shown below: 感谢codingbiz的共鸣 ,我得以实现如下所示的脚本:

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
    DateTime thisTime = DateTime.Now;           // retrieve date and time
    string day = thisTime.DayOfWeek.ToString(); // declare and define day to string 
    int hour = thisTime.Hour;                   // declare and define hour to int

//check if string contains the following: "Monday,Tuesday,Wednesday,Thursay,Friday"
if ("Monday,Tuesday,Wednesday,Thursay,Friday".IndexOf(day) > -1 && hour >= 9 && hour <= 17)
{
   conditionalLabel.Text = "<img src='sony_frontex3_repair.jpg' />";
}
}
</script>

I also added the following conditional label to the body of the content page and master page: 我还将以下条件标签添加到了内容页和母版页的正文中:

New question: Lets say a user visits my website at 4:45PM and the image/string displays as intended. 新问题:假设某个用户在下午4:45访问我的网站,并且图像/字符串按预期显示。 Some time goes by and it is now 5:05. 一段时间过去了,现在是5:05。 Will the image/string automatically disappear, or only after the page is closed, then reopened? 图像/字符串会自动消失,还是仅在页面关闭后才重新打开? Will cookies or cache affect the performance of this script? Cookie或缓存会影响此脚本的性能吗? Is there any such issues I need to address, such as specifying in the code to automatically refresh the page? 我是否需要解决任何此类问题,例如在代码中指定以自动刷新页面?

You can make use of Hidden field in the aspx page. 您可以在aspx页面中使用“隐藏”字段。 Load the required string value from code behind into the hidden field and then make use of that field in the javascript function to display the value. 从代码后面将所需的字符串值加载到隐藏字段中,然后利用javascript函数中的该字段显示该值。

It is easy to do with ASP.NET and C#. 使用ASP.NET和C#很容易。 You can also do it using JavaScript but I won't recommend it as you will be dealing with client's date and time. 您也可以使用JavaScript来完成此操作,但由于您要处理客户的日期和时间,因此我不建议您这样做。 To show it using ASP.net you may take advantage of an ASP.net control called PlaceHolder . 要使用ASP.net进行显示,您可以利用称为PlaceHolder的ASP.net控件。 You can easily put controls or a simple text or even an HTML markup inside a PlaceHolder. 您可以轻松地将控件或简单文本甚至HTML标记放入PlaceHolder中。 It's been a while since I wrote ASP.net WebForms so excuse me if there is any problems with my code but it shows you the general idea. 自从我写ASP.net WebForms以来已经有一段时间了,所以如果我的代码有任何问题,请原谅,但是它向您展示了总体思路。 Put

<asp:PlaceHolder Id="MessageHolder" runat="Server" />

in your .aspx page and in the C# code at the page load event put something like this 在您的.aspx页和页面加载事件中的C#代码中,输入如下内容

void Page_Load(...) {
   if (DateTime.Now > StartDate && DateTime.Now < EndDate) {
      MessageHolder.Controls.Add(...);
   }
}

If you simply want to add HTML tags inside and not ASP.net controls you can use a class called HtmlGenericControl to generate your HTML tags and then add them inside the PlaceHolder. 如果只想在内部添加HTML标记而不是ASP.net控件,则可以使用名为HtmlGenericControl的类来生成HTML标记,然后将其添加到PlaceHolder中。

If you want to put a text in your page at that specific time instead of using PlaceHolder you might use Literal control and simply say: 如果要在该特定时间在页面中放置文本而不是使用PlaceHolder,则可以使用文字控件,并简单地说:

LiteralControl.Text = "<p>My special message!</p>";

Regards, 问候,
Shakib 沙基卜

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

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