如何在 ASP.NET 控件中添加“if”语句?
<asp:Label ID="lblBeginDate" runat="server" Text='<%# ((DateTime)Eval("beginDate")).ToShortDateString() %>'></asp:Label>
如果日期为空,则将文本设置为“未选择日期”
我已经尝试过了,但无法让它工作。
<asp:Label ID="lblBeginDate" runat="server" Text='<%# ((DateTime)Eval("beginDate")) != null ? ((DateTime)Eval("beginDate")).ToShortDateString() : "No Date Selected" %>'></asp:Label>
--错误我的上述声明“指定的演员表无效。”
将 Gridview 与数据集一起用作从 SQL 数据库填充的数据源。
更新——找到我想做的事。参考
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%if (i == 1) { %>
<asp:Label ID="Label1" runat="server" Text="If Bloc"></asp:Label><br />
<%} %>
<%else { %>
<asp:Label ID="Label2" runat="server" Text="Else Block"></asp:Label>
<%} %>
</div>
</form>
</body>
</html>
或者
<asp:Label ID="lblBeginDate1" runat="server" Text='<%# Eval("beginDate").ToString().Length > 0 ? ((DateTime)Eval("beginDate")).ToShortDateString():"Not Selected Yet" %>' />