在页面和Datalist控件中判断是否为今天
研究了一天,查了很多资料,终于搞明白了,呵呵
前台代码:
后台CS代码:
欢迎各位支出不足之处或更好的处理方法,谢谢
前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ifnow.aspx.cs" Inherits="TEST_ifnow" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>在页面和Datalist控件中判断是否为今天</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate>
ID:
<asp:Label ID="ArticlsIDLabel" runat="server" Text='<%# Eval("ID") %>'></asp:Label><br />
Title:
<asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>'></asp:Label><br />
Content:
<asp:Label ID="ContentLabel" runat="server" Text='<%# Eval("Content") %>'></asp:Label><br />
AddTime:
<asp:Label ID="AddTimeLabel" runat="server" Text='<%# Eval("AddTime") %>'></asp:Label><br />
<br />
</ItemTemplate>
</asp:DataList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NewWebSiteConnectionString %>"
SelectCommand="SELECT * FROM [Articl]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>在页面和Datalist控件中判断是否为今天</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate>
ID:
<asp:Label ID="ArticlsIDLabel" runat="server" Text='<%# Eval("ID") %>'></asp:Label><br />
Title:
<asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>'></asp:Label><br />
Content:
<asp:Label ID="ContentLabel" runat="server" Text='<%# Eval("Content") %>'></asp:Label><br />
AddTime:
<asp:Label ID="AddTimeLabel" runat="server" Text='<%# Eval("AddTime") %>'></asp:Label><br />
<br />
</ItemTemplate>
</asp:DataList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NewWebSiteConnectionString %>"
SelectCommand="SELECT * FROM [Articl]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
后台CS代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class TEST_ifnow : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToLongDateString();
string dt = Label1.Text;
string mydt1 = "2007-9-13 15:35:13";
DateTime mydt = Convert.ToDateTime(mydt1);
//DateTime dt=(DateTime)Label1.Text;
if (mydt.ToLongDateString()== DateTime.Now.Date.ToLongDateString())
{
Response.Write("今天");
}
else
{
Response.Write("不是今天");
}
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemIndex > -1)
{
string time = ((Label)e.Item.FindControl("AddTimeLabel")).Text;
DateTime dt = Convert.ToDateTime(time);
if (dt.ToShortDateString() == DateTime.Now.Date.ToShortDateString())
{
((Label)e.Item.FindControl("AddTimeLabel")).Text = "今天";
}
else
{
((Label)e.Item.FindControl("AddTimeLabel")).Text = "以前";
}
//((Label)e.Item.FindControl("Label3")).Text = aaa;
//Label2.Text = ((Label)e.Item.FindControl("Label3")).Text;
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class TEST_ifnow : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToLongDateString();
string dt = Label1.Text;
string mydt1 = "2007-9-13 15:35:13";
DateTime mydt = Convert.ToDateTime(mydt1);
//DateTime dt=(DateTime)Label1.Text;
if (mydt.ToLongDateString()== DateTime.Now.Date.ToLongDateString())
{
Response.Write("今天");
}
else
{
Response.Write("不是今天");
}
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemIndex > -1)
{
string time = ((Label)e.Item.FindControl("AddTimeLabel")).Text;
DateTime dt = Convert.ToDateTime(time);
if (dt.ToShortDateString() == DateTime.Now.Date.ToShortDateString())
{
((Label)e.Item.FindControl("AddTimeLabel")).Text = "今天";
}
else
{
((Label)e.Item.FindControl("AddTimeLabel")).Text = "以前";
}
//((Label)e.Item.FindControl("Label3")).Text = aaa;
//Label2.Text = ((Label)e.Item.FindControl("Label3")).Text;
}
}
}
欢迎各位支出不足之处或更好的处理方法,谢谢