如何填写DropDown一个月的周数和日期

时间:2013-07-09 02:56:30

标签: vb.net

Example

  1. 选择月份&年
  2. 使用星期数和月份日期填写dropdownllist
  3. 例如,选择第4个月(4月),WeekNo的输出将如下:

    Week No  Date
    Week 1   [1-7]
    week 2   [8-14]
    Week 3   [15-21]
    week.....
    

2 个答案:

答案 0 :(得分:2)

这是执行该操作的完整c#代码

protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            LoadMonthDdl();
        }
    }

    private void LoadMonthDdl()
    {
        ddlMotnh.Items.Clear();
        ddlMotnh.Items.Add(new ListItem("January-"+DateTime.Now.Year,"1"));
        ddlMotnh.Items.Add(new ListItem("February-" + DateTime.Now.Year, "2"));
        ddlMotnh.Items.Add(new ListItem("March-" + DateTime.Now.Year, "3"));
        ddlMotnh.Items.Add(new ListItem("April-" + DateTime.Now.Year, "4"));
        ddlMotnh.Items.Add(new ListItem("May-" + DateTime.Now.Year, "5"));
        ddlMotnh.Items.Add(new ListItem("June-" + DateTime.Now.Year, "6"));
        ddlMotnh.Items.Add(new ListItem("July-" + DateTime.Now.Year, "7"));
        ddlMotnh.Items.Add(new ListItem("August-" + DateTime.Now.Year, "8"));
        ddlMotnh.Items.Add(new ListItem("Septeber-" + DateTime.Now.Year, "9"));
        ddlMotnh.Items.Add(new ListItem("October-" + DateTime.Now.Year, "10"));
        ddlMotnh.Items.Add(new ListItem("November-" + DateTime.Now.Year, "11"));
        ddlMotnh.Items.Add(new ListItem("December-" + DateTime.Now.Year, "12"));

    }
    protected void weekWithDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlweekWithDate.Items.Clear();
        int month = Convert.ToInt32(ddlMotnh.SelectedValue);
        DateTime beginDate= new DateTime(DateTime.Now.Year,month,1);
        int beginDay = beginDate.Date.Day;
        int noOfDay = System.DateTime.DaysInMonth(DateTime.Now.Year, month);
        int weekNo=1;
        int weekStartDate = 1;
        string str = "";
        while (beginDay<=noOfDay)
        {

            str = "week " + weekNo.ToString()+"["+weekStartDate;
            int newWeekN0 = GetWeekOfMonth(beginDate);
            if(weekNo==newWeekN0)
            {
            }
            else
            {
                str += "-" + beginDate.AddDays(-1).Day + "]";
                ddlweekWithDate.Items.Add(new ListItem(str, newWeekN0.ToString()));
                weekNo = newWeekN0;
                weekStartDate = beginDate.Date.Day;
            }
            beginDate = beginDate.AddDays(1);
            beginDay++;
        }
        if(!str.Contains("]"))
        {
            str += "-" + noOfDay + "]";
            ddlweekWithDate.Items.Add(new ListItem(str, weekNo.ToString()));
        }


    }
    public static int GetWeekOfMonth(DateTime date)
    {
        DateTime beginningOfMonth = new DateTime(date.Year, date.Month, 1);

        while (date.Date.AddDays(1).DayOfWeek != CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek)
            date = date.AddDays(1);

        return (int)Math.Truncate((double)date.Subtract(beginningOfMonth).TotalDays / 7f) + 1;
    }

这是html

<asp:DropDownList runat="server" ID="ddlMotnh" AutoPostBack="True" onselectedindexchanged="weekWithDate_SelectedIndexChanged"/>
    <asp:DropDownList runat="server" ID="ddlweekWithDate" 
        />

这很好用。只需将此c#代码转换为VB用于您的目的

答案 1 :(得分:0)

使用自定义格式为“MMMM-yyyy”的DateTimePicker将允许选择月份和年份。然后处理ValueChanged事件将以这样的简单方式填充组合框:

Private Sub DateTimePicker1_ValueChanged(sender As System.Object, e As _
         System.EventArgs) Handles DateTimePicker1.ValueChanged

    Dim PickedDate As New DateTime(DateTimePicker1.Value.Year, _
        DateTimePicker1.Value.Month, 1)

    'Offset to the start of the second week
    Dim FirstWeek As Integer = 7 - PickedDate.DayOfWeek
    ComboBox1.Items.Clear()
    ComboBox1.Text = "Select Week No. and Dates"
    'Add first week to CB
    ComboBox1.Items.Add("Week 1 [1-" + FirstWeek.ToString + "]")
    'Point PickedDate to the start of the second week
    PickedDate = PickedDate.AddDays(FirstWeek)
    'The number of days left in the month
    Dim MonthDays As Integer = DateTime.DaysInMonth(PickedDate.Year, _
         PickedDate.Month) - PickedDate.Day

    'Loop to add the rest of the CB items
    For I = 0 To MonthDays Step 7
        Dim FromDate As Integer = PickedDate.Day + I
        Dim ToDate As Integer = PickedDate.Day + I + 6
        If ToDate > MonthDays + PickedDate.Day Then ToDate = MonthDays + _
            PickedDate.Day

        ComboBox1.Items.Add("Week " + (CInt(I / 7) + 2).ToString + " [" + _
            FromDate.ToString + "-" + ToDate.ToString + "]")

    Next
End Sub