Asp. NET implementation of the month and day selection function

Source: Internet
Author: User

ASP. 3.5 (C #) Practical Tutorial 4.8 The first question, the use of 3 DropDownList control to achieve the day and month date selection function. Effect:

  To do this, solve the following problems:

1. Consider the number of days per month, 4,6,9,11 month has 30 days, 1,3,5,7,8,10,12 month has 31 days, common year February 28 days, leap year February 29 days.

2. Consider real-time display, that is, after the year or month has been modified, the option of ' Day ' must be changed in time.

  The solution is as follows, including a series of code snippets, complete source code please click here to download.

  1. Initialize the page so that DropDownList displays the current date and year.

page initialization , add the corresponding items to the DropDownList of the year and month, and let the current year and month be selected. The DropDownList is then added ListItem based on the current selected year and month to add the display day. The last setting of the current day is selected.

    protected void init_date ()      // initializes the date and changes the original display date to the current date     {        set_year ();        Set_month ();         true ;         true ;        Update_date ();         true ;    }
init_date ()

  

Add year option, from 1900 to current year.

    protected void set_year ()   // set year, from 1900 to now     {        for (int  1900; I <= DateTime.Now.Year; i++)        {year            . Items.Add (new  ListItem (i.ToString (), i.ToString ()));        }    }
set_year ()

  

Add month option, January to December.

    protected void set_month ()  // set month, 1-12    {        for (int1  ; i++)        {            month. Items.Add (new  ListItem (i.ToString (), i.ToString ()));        }     }
Set_month ()

Add day option, which is added according to the selected year and month. First gets the current selected year and month, calculates the number of days of the month, and then adds.

Gets the number of days of the month according to date.

    protected intGet_days (intYeintOok//gets the number of days in the month based on year and month    {        if(Mo = =4|| Mo = =6|| Mo = =9|| Mo = = One)//30-Day Month        {            return  -; }        Else if(Mo = =2)        {            if(ye%4==0)//Leap Year February            {                return  in; }            Else       //Common year February            {                return  -; }        }        Else           //31-Day Month        {            return  to; }    }
get_days (int ye,int mo)

Adds a day option based on the number of days.

    protected voidSet_day (intMax//set Day, parameter is the maximum value to set    {        intCount =Day .        Items.Count; if(count>max) {             for(inti=count;i>max;i--) {Day. Items.remove (day.            Items.findbytext (i.ToString ())); }        }        Else if(Count <max) {             for(inti=count+1; i<=max;i++) {Day. Items.Add (NewListItem (i.ToString (), i.ToString ())); }        }    }
set_day (int max)

The update day option, which is invoked once during initialization, is triggered when the year option or the month option changes.

    protected void update_date ()    // Update ' Day '    {        intint. Parse (year. Selecteditem.text);         int int . Parse (month. Selecteditem.text);         int days = get_days (ye,mo);        Set_day (days);    }
update_date ()

   2. Real-time update DropDownList option, the current year and month changes when the client should immediately send back to the server page, and then the server to send the latest page to the client. To achieve the purpose of the update.

Set the 3 DropDownList AutoPostBack to true in page, so that once the options change, the server can send the most recent page back to the browser.

            <asp:dropdownlistID= "Year"runat= "Server"onselectedindexchanged= "Year_selectedindexchanged"AutoPostBack= "true"></asp:dropdownlist>years<asp:dropdownlistID= "Month"runat= "Server"onselectedindexchanged= "Month_selectedindexchanged"AutoPostBack= "true"></asp:dropdownlist>Month<asp:dropdownlistID= "Day"runat= "Server"AutoPostBack= "true"></asp:dropdownlist>Day<Asp:labelID= "Show_txt"runat= "Server"Text=""></Asp:label>
View Code

At the same time, when the OnSelectedIndexChanged event is triggered when the two options for the year and month are changed, the option for the more-than-day.

    // Update every time you change the year and month    protected void year_selectedindexchanged (object  sender, EventArgs e)    {        update_date ();    }     protectedvoid month_selectedindexchanged (object  sender, EventArgs e)    {        update_date ();    }
View Code

The above is to solve this problem of ideas, are some code snippets, need complete source code please click here to download.

Asp. NET implementation of the month and day selection function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.