Você está na página 1de 3

Date Code: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.

WebControls; using System.Data; using System.Configuration; using System.Data.SqlClient; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls.WebParts; using System.Collections; namespace WebApplication1 { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { int year, month; DateTime tnow = DateTime.Now; ArrayList AlYear = new ArrayList(); int i; for (i = 2012; i <= 2020; i++) AlYear.Add(i); ArrayList AlMonth = new ArrayList(); for (i = 1; i <= 12; i++) AlMonth.Add(i); if (!this.IsPostBack) { years.DataSource = AlYear; years.DataBind(); years.SelectedValue = tnow.Year.ToString(); months.DataSource = AlMonth; months.DataBind(); months.SelectedValue = tnow.Month.ToString(); year = Int32.Parse(years.SelectedValue); month = Int32.Parse(months.SelectedValue); BindDays(year, month); Day.SelectedValue = tnow.Day.ToString(); } } //judge leap yearz private bool CheckLeap(int year) { if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) return true; else return false; } //binding every month day private void BindDays(int year, int month) { int i; ArrayList AlDay = new ArrayList();

switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: for (i = 1; i <= 31; i++) AlDay.Add(i); break; case 2: if (CheckLeap(year)) { for (i = 1; i <= 29; i++) AlDay.Add(i); } else { for (i = 1; i <= 28; i++) AlDay.Add(i); } break; case 4: case 6: case 9: case 11: for (i = 1; i <= 30; i++) AlDay.Add(i); break; } Day.DataSource = AlDay; Day.DataBind(); } //select year public void DropDownList1_SelectedIndexChanged(object sender, System.EventAr gs e) { int year = Int32.Parse(years.SelectedValue); int month = Int32.Parse(months.SelectedValue); BindDays(year, month); } //select month public void DropDownList2_SelectedIndexChanged(object sender, System.EventAr gs e) { int year = Int32.Parse(years.SelectedValue); int month = Int32.Parse(months.SelectedValue); BindDays(year, month); } protected void Button1_Click(object sender, EventArgs e) { Label1.Text = Day.SelectedValue + "/" + months.SelectedValue + "/" + ye ars.SelectedValue; SqlConnection connection = new SqlConnection("Data Source=localhost;Init ial Catalog=listbox;Integrated Security=True"); connection.Open();

string query = " Insert into date (Date) values ('"+Label1.Text+"')"; SqlCommand command = new SqlCommand(query, connection); command.ExecuteNonQuery(); connection.Close(); }

} }

Você também pode gostar