gpt4 book ai didi

c# - 如何在 GridView 的 RowDataBound 中使用模板字段项

转载 作者:行者123 更新时间:2023-11-30 23:33:43 26 4
gpt4 key购买 nike

ASP.net:

...
<asp:BoundField HeaderStyle-Width="7%" DataField="Due Date" HeaderText="Due" SortExpression="Due Date" ItemStyle-CssClass="taskTableColumn" />
...

C# 代码(在 RowDataDound 中用于 GridView):

if (!string.IsNullOrEmpty(e.Row.Cells[4].Text)) //works correctly
{
if (DateTime.Parse(e.Row.Cells[4].Text).Date < DateTime.Now.Date)
{
e.Row.ForeColor = Color.FromName("#C00000");
e.Row.ToolTip = "Task is Past Due";
}
else if (DateTime.Parse(e.Row.Cells[4].Text).Date <= DateTime.Now.AddDays(inDateOffset).Date)
{
//e.Row.ForeColor = Color.FromName("#8A8C00");
e.Row.ToolTip = "Task is at Risk";
}
else
{
e.Row.Cells[5].ToolTip = "Task is Not Due Yet";
}
}

上面的 ASP.net/C# 代码工作正常。

我不得不修改 ASP.net 以将字段格式化为日期:

<asp:TemplateField HeaderStyle-Width="7%" HeaderText="Due" SortExpression="Due Date" ItemStyle-CssClass="taskTableColumn">
<ItemTemplate>
<asp:Label ID="lblDue" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Due Date", "{0:MM-dd-yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

现在下面的行总是返回一个空字符串:if (!string.IsNullOrEmpty(e.Row.Cells[4].Text))

如何修改 C# 代码,以便它能够确定日期是否已过期。

最佳答案

因为您现在使用的是 <asp:TemplateField> ,您无法再以相同的方式获取文本。您必须找到实际的标签控件。而不是使用 e.Row.Cells ,您将使用将数据绑定(bind)到的标签。

Label lblDue = (Label)e.Row.FindControl("lblDue");
if (!string.IsNullOrEmpty(lblDue.Text))
{
...
}

关于c# - 如何在 GridView 的 RowDataBound 中使用模板字段项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33812570/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com