我正在处理一些旧代码(不是我的),不幸的是,其中有很多硬编码代码,这导致了我现在正在尝试解决的问题。这是我现在的问题:
有一个 14 列代表两周的网格视图。第一天是星期一或星期日,具体取决于代码中的布尔值,用于检查用户的“类型”。
现在,所有的日子都是这样安排的(这是星期天):
<asp:TemplateField HeaderText="SUN">
<footertemplate>
<asp:Label ID="lblD1F" runat="server" ForeColor="white" Width="35px" Text="<%# GetTotal(0).ToString() %>" />
</footertemplate>
<headertemplate>
<asp:Label ID="lblD1H" runat="server" CssClass="hdr_Day" Text="SUN"></asp:Label><br />
<asp:Label ID="lblD1D" runat="server" CssClass="hdr_Date" Text='<%# _displayDate.ToString("MM/dd") %>'></asp:Label>
</headertemplate>
<itemtemplate>
<anthem:TextBox id="tbDay1"
runat="server"
Text='<%# Bind("Day1") %>'
CssClass="tbWeekEnd"
AutoCallBack="true" />
<asp:Label ID="lblDay1" runat="server" Visible="false" Text='<%# Bind("Day1") %>'></asp:Label>
</itemtemplate>
<itemstyle cssclass="cell_weekend" />
</asp:TemplateField>
因此,像上面这样设置 14 天,结果如下:
现在,我试图让它根据用户在周日或周一开始。我几乎在旧字符串之上硬编码了新字符串,这刚刚开始引起更多问题。首先,我制作了两个常量字符串:
String[] userADays = new String[] { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
String[] userBDays = new String[] { "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN" };
protected void gvMasterProjects_RowDataBound(object sender, GridViewRowEventArgs e)
{
//This if/else statement overrides the hard coded date headers in the timeEntry aspx files since the days were hardcoded in.
//Populates the cells w/ the data from one of two arrays depending if the user is A or B.
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.CssClass = "HeaderStyle";
if (isUserB == false)
{
for (int i = 9; i < 23; i++)
{
e.Row.Cells[i].Font.Size = 10;
e.Row.Cells[i].Text = userADays[i - 9] + "\n" + " " + _displayDate.AddDays(i-9).ToString("MM/dd") + " ";
}
}
else
{
for (int i = 9; i < 23; i++)
{
e.Row.Cells[i].Font.Size = 10;
e.Row.Cells[i].Text = UserBDays[i - 9] + "\n" + " " + _displayDate.AddDays(i - 9).ToString("MM/dd") + " ";
}
}
}
这是 .aspx 文件中的 gridview 内容:
<anthem:GridView
ID="gvMasterProjects"
runat="server"
AutoGenerateColumns="false"
ShowFooter="true"
OnRowDataBound="gvMasterProjects_RowDataBound"
AllowPaging="false"
EnableViewState="true"
HorizontalAlign="Center"
AlternatingRowStyle-CssClass="AlternatingRowStyle"
RowStyle-CssClass="RowStyle"
HeaderStyle-CssClass="HeaderStyle" style="margin-left: 71px">
我真的很想取消这种硬编码。必须有一种更简单的方法来加载这些日期,而无需执行当前正在执行的操作。任何帮助将不胜感激。
编辑:尝试了下面的答案,除了执行许多“UpdateAfterCallBack”行会导致日期恢复为.aspx中的硬编码日期之外,这些答案都有效。在 .aspx 中是否有一种简单的方法可以做到这一点?