5

DefaultValue次のコードの 's を現在の月の開始日(最初の ControlParameter)と最後の日付(2 番目の ControlParameter)に設定するにはどうすればよいですか?

<SelectParameters>
    <asp:ControlParameter ControlID="txtFromDate" Name="ExpenseDate" PropertyName="Text"
         Type="String" DefaultValue="01-05-2013" ConvertEmptyStringToNull="true" />
    <asp:ControlParameter ControlID="txtToDate" Name="ExpenseDate2" PropertyName="Text" 
         Type="String" DefaultValue="30-05-2013" ConvertEmptyStringToNull="true" />
</SelectParameters>
4

4 に答える 4

16
DateTime today = DateTime.Today;
int daysInMonth = DateTime.DaysInMonth(today.Year, today.Month);

DateTime startOfMonth = new DateTime(today.Year, today.Month, 1);    
DateTime endOfMonth = new DateTime(today.Year, today.Month, daysInMonth);

次に、これらの値をコントロールに設定できます。

于 2013-05-14T18:53:12.533 に答える
4
DateTime now = DateTime.Now;
this.txtFromDate.Text = New DateTime(now.Year, now.Month, 1).ToString("dd-MM-yyyy");

DateTime lastDayOfMonth = now.AddMonths(1).AddDays(-1);
this.txtToDate.Text = lastDayOfMonth.ToString("dd-MM-yyyy");

私はこれを記憶からやっています。間違いやタイプミスがあれば申し訳ありませんが、それに近いものです。

于 2013-05-14T18:52:10.490 に答える
0

これらのシナリオに対処するために、いくつかの拡張メソッドを自分で作成しました。

public static class DateTimeExtensionMethods
{
        /// <summary>
        /// Returns the first day of the month for the given date.
        /// </summary>
        /// <param name="self">"this" date</param>
        /// <returns>DateTime representing the first day of the month</returns>
        public static DateTime FirstDayOfMonth(this DateTime self)
        {
            return new DateTime(self.Year, self.Month, 1, self.Hour, self.Minute, self.Second, self.Millisecond);
        }   // eo FirstDayOfMonth


        /// <summary>
        /// Returns the last day of the month for the given date.
        /// </summary>
        /// <param name="self">"this" date</param>
        /// <returns>DateTime representing the last of the month</returns>
        public static DateTime LastDayOfMonth(this DateTime self)
        {
            return FirstDayOfMonth(self.AddMonths(1)).AddDays(-1);
        }   // eo LastDayOfMonth
}
于 2013-05-14T19:03:12.090 に答える