惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
罗磊的独立博客
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Last Week in AI
Last Week in AI
云风的 BLOG
云风的 BLOG
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
A
About on SuperTechFans
WordPress大学
WordPress大学
B
Blog
Martin Fowler
Martin Fowler
Jina AI
Jina AI
I
InfoQ
P
Proofpoint News Feed
小众软件
小众软件
S
SegmentFault 最新的问题
V
V2EX
B
Blog RSS Feed
量子位
大猫的无限游戏
大猫的无限游戏
aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
MongoDB | Blog
MongoDB | Blog
美团技术团队

博客园 - 星空竹月

Div中文字竖直居中的问题 ASP入门基础教程-VBScript概述及基本元素 第一次安装sql2000挂起无法安装的问题 asp.net(c#)成条形码[转] 关于文章列表如何直接显示文章类别名的问题 Page_PreInit在网页传值的应用 利用.net的强大功能发送email - 星空竹月 - 博客园 如何在asp.net中关闭B页面时,自动刷新A页面? - 星空竹月 - 博客园 ASP.net(c#)做返回上一页效果(后退)--代码 前进和后退按钮特效代码 - 星空竹月 - 博客园 文件上传实例(文件上传,自动重命名,自动添加文件夹) ASP.NET中文显示乱码之解决方法 限制显示字数 asp.net中,限制显示字数的问题? 将截断字符串或二进制数据。 IE6下透明PNG图片的显示 IE不支持PNG图片透明效果,怎么办 配置AjaxControlToolkit 怎样换网页中的鼠标图案??换成其他图片
在页面和Datalist控件中判断是否为今天 - 星空竹月 - 博客园
星空竹月 · 2007-09-13 · via 博客园 - 星空竹月

研究了一天,查了很多资料,终于搞明白了,呵呵
前台代码:

<%@ 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>

后台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;
        }

    }
}

欢迎各位支出不足之处或更好的处理方法,谢谢