将硬编码日期更改为动态

Changing hardcoded dates to dynamic

我正在处理一些旧代码(不是我的),不幸的是,其中有很多硬编码代码,这些代码一直在导致我现在正在尝试修复的问题。这是我现在的问题:

有一个 gridview 有 14 列代表两周。第一天是星期一还是星期日,具体取决于检查 "type" 用户的代码中的布尔值。

现在,所有的日子都这样安排(这是星期天):

    <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">

我真的很想取消这种硬编码。必须有一种更简单的方法来加载这些日期 w/o 做现在正在做的事情。任何帮助将不胜感激。

编辑:尝试了下面的答案,除了有许多 "UpdateAfterCallBack" 行被执行导致日期被恢复为 .aspx 中的硬编码日期外,这些答案有效。有没有在 .aspx 中执行此操作的简单方法?

您是否考虑过只在其中设置 "buffer" 天,以便如果用户属于您想要的类型,您可以在开始日上添加一天?

这样做是创建一个缓冲区,默认情况下为 0,如果是特定类型的用户则为 1。然后添加进去,你就有了约会对象。

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";
        var bufferDay = 0; // Starts on sunday.
        if (isUserB)
        {
            bufferDay = 1; // Starts on Monday.
        }
        for (int i = 9; i < 23; i++)
        {
            e.Row.Cells[i].Font.Size = 10;
            var dayOfWeek = _displayDate.AddDays(i - 9 + bufferDay);
            e.Row.Cells[i].Text = dayOfWeek.ToString("ddd") + "\n" + " " + dayOfWeek.ToString("MM/dd") + " ";
        }        
    }
}

根据您的编辑:如果您像这样编辑 .aspx 以便它根据提供的日期提取日期文本,会发生什么情况?我不确定您是否必须为 HeaderText 添加对 _displayDate 的引用(自从我弄乱 aspx 以来已经有一段时间了)。我不确定在您将数据调用到 HeaderText 中的 sub 时数据是否会被绑定。

  <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='<%# _displayDate.ToString("mmm") %>'></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>

我个人甚至会研究一种自动化 TemplateFields 的方法。看起来你有很多重复的代码,你可以从 class 创建,就像这个线程的其他答案中提到的那样。但请尝试以上操作,看看是否能解决您的更新问题。

Here is a SO thread that explains setting the initial day of the week. It involves creating a "custom culture." Note that the DateTimeFormatInfo class 有一个 FirstDayOfWeek 属性。我想您可以创建 2 个实例,每个实例都从不同的一天开始。也许自定义文化有点多,无论如何有一个 FirstWeekDay 属性 某处 你的代码集。

设置后,查看 DateTime 格式,以便您可以通过遍历 DayOfWeek enumeration and outputting it in your desired format

输出 "SUN"、"MON" 等

以上所有内容都应封装在一个class中,因此只需调用简单的方法或属性即可。它会真正清理您显示的 "level" 处的编码;当然,它是可重复使用的。但同样,我正在为每个星期的第一天想象一个实例,但我怀疑大部分代码可能是 static.

最后,this is cool.