如何使用asp.net代码在表中倒计时? [英] How to put a countdown in a table using asp.net code?

查看:121
本文介绍了如何使用asp.net代码在表中倒计时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为adverts的表,其中包含以下字段:advert_id,name,date_created description,duration,status,user_id,cat_id。



假设我们说一个用户在持续时间字段中输入1周的持续时间,并将其状态设置为活动状态。一周过后,表中的状态设置为无效。



任何人都可以告诉我是否可以使用visual使用asp.net 4.0代码执行此操作Visual Studio 2010中基本的Web表单?

Lets say I have a table called adverts with the following fields:advert_id, name,date_created description,duration,status,user_id, cat_id.

Let's say a user puts a duration of 1 week in the duration field and its status is set to active. After a week has passed, the status is set to inactive in the table.

Can anyone tell me if it's possible to do this with asp.net 4.0 code using visual basic in visual studio 2010 with web forms?

推荐答案

您不一定需要在数据库表中有一个列的状态,您可以使用聚合列你从表中查询。

这样就不需要更新表了。



advert_id name date_created description duration user_id cat_id
1 无论 2014- 10-15 状态将无效 P7D 5 8
1 无论 2014-10-25 状态是否有效 P7D 7 9




注意对于dur我正在使用 xsd:duration [ ^ ]格式,列类型应为VARCHAR(25)。



在您的查询中,您可以使用计算来聚合状态。

因为我主要在MySQL工作,所以我使用这种语法,但应该可以将代码转换为其他SQL风格。

You don't necessarily need to have a column in your database table for status, you could use an aggregated column when you do queries from the table.
In this way there is no need update the table.

advert_idnamedate_createddescriptiondurationuser_idcat_id
1Whatever2014-10-15The status will be inactiveP7D58
1Whatever2014-10-25The status will be activeP7D79


Note For duration I am using xsd:duration[^] format and the column type should be VARCHAR(25).

In your query you can aggregate the status using a calculation.
As I mostly work in MySQL I use this syntax, but it should be possible to translate the code to other SQL flavors.
SELECT `name`, IF(DATE_SUB(CURDATE(), INTERVAL TRIM(TRAILING 'D' FROM SUBSTRING(duration, 2)) DAY) > date_created, 'Inactive', 'Active') AS `status` FROM adverts;



(获得持续时间的方式可以改善。)



你也可以在c#中执行此操作。


(The way to get the number of days out of duration can be improved.)

You can also do this in c#.

DataTable dt = new DataTable();
dt.RowChanged += dt_RowChanged; // Add an event that fires for every new row

dt.Columns.Add("advert_id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("date_created", typeof(DateTime));
dt.Columns.Add("duration", typeof(TimeSpan));
dt.Columns.Add("status", typeof(string));

// Here you would use something like
//DbDataAdapter da = new SqlDataAdapter();
//da.SelectCommand = new SqlCommand("SELECT advert_id, name, date_created, duration, '' AS status FROM adverts");
//da.Fill(dt);

// Instead I add the rows manually
dt.Rows.Add(1, "Row1", DateTime.Now.Subtract(new TimeSpan(10, 0, 0, 0, 0)), new TimeSpan(7, 0, 0, 0, 0));
dt.Rows.Add(2, "Row2", DateTime.Now, new TimeSpan(7, 0, 0, 0, 0));





事件处理程序



The event handler

static void dt_RowChanged(object sender, DataRowChangeEventArgs e)
{
    if (e.Action == DataRowAction.Add)
    {
        DateTime created = (DateTime)e.Row["date_created"];
        TimeSpan duration = (TimeSpan)e.Row["duration"];
        e.Row["status"] = (created > DateTime.Now.Subtract(duration)) ? "Active" : "Inactive";
    }
}


完美答案。非常感谢!!!
Perfect answer. Thank you very much!!!


这篇关于如何使用asp.net代码在表中倒计时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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