Live Chat Icon For mobile
Live Chat Icon

How to change the Calendar Dates using a Dropdownlist

Platform: ASP.NET| Category: Calendar

<asp:DropDownList id='ddlMonth' style='Z-INDEX: 101; LEFT: 64px; POSITION: absolute; TOP: 24px' runat='server'
	AutoPostBack='True'></asp:DropDownList>
<asp:Label id='Label1' style='Z-INDEX: 103; LEFT: 8px; POSITION: absolute; TOP: 24px' runat='server'>Month</asp:Label>
<asp:DropDownList id='ddlYear' style='Z-INDEX: 102; LEFT: 200px; POSITION: absolute; TOP: 24px' runat='server'
	AutoPostBack='True'></asp:DropDownList>
<asp:Label id='Label2' style='Z-INDEX: 104; LEFT: 152px; POSITION: absolute; TOP: 24px' runat='server'>Year</asp:Label>
<asp:Calendar id='Calendar1' style='Z-INDEX: 105; LEFT: 16px; POSITION: absolute; TOP: 72px' runat='server'></asp:Calendar>

VB.NET


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	’ Put user code to initialize the page here
	If Not Page.IsPostBack Then
		’Populate month in the dropdownlist
	           	Dim strMonth As String = ''
		Dim i As Integer
		            For i = 1 To 12
		                If i.ToString().Length < 2 Then
				strMonth = '0' + i.ToString()
			                ddlMonth.Items.Add(New ListItem(strMonth, strMonth))
		                Else
	                	ddlMonth.Items.Add(New ListItem(strMonth, strMonth))
  		              	End If
		            Next

	            	ddlMonth.Items.FindByValue(DateTime.Now.ToString('MM')).Selected = True
            		’Populate year in the dropdownlist
		Dim j As Integer
		For j = 1900 To 2050
		                ddlYear.Items.Add(New ListItem(j.ToString(), j.ToString()))
		Next
		ddlYear.Items.FindByText(DateTime.Now.ToString('yyyy')).Selected = True
	End If
End Sub ’Page_Load


Private Sub ddlMonth_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlMonth.SelectedIndexChanged
	        SetCalendarDate()
End Sub ’ddlMonth_SelectedIndexChanged

Private Sub ddlYear_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlYear.SelectedIndexChanged
  	      SetCalendarDate()
End Sub ’ddlYear_SelectedIndexChanged

Sub SetCalendarDate()
  	Dim dtNewDate As DateTime
	dtNewDate = DateTime.Parse((Int16.Parse(ddlMonth.SelectedItem.Text) & '/1/' & Int16.Parse(ddlYear.SelectedItem.Text)))
	Calendar1.TodaysDate = dtNewDate
End Sub ’SetCalendarDate

C#


private void Page_Load(object sender, System.EventArgs e)
{
	// Put user code to initialize the page here
	if (!Page.IsPostBack )
	{
		//Populate month in the dropdownlist
		string strMonth='';
		for(int i = 1 ;i<=12;i++)
		{
			if (i.ToString().Length <2 )
			{
				strMonth ='0' + i.ToString ();
				ddlMonth.Items.Add (new ListItem(strMonth,strMonth )) ;
			}
			else
			{
				ddlMonth.Items.Add (new ListItem(strMonth,strMonth )) ;
			}
		}
		ddlMonth.Items.FindByValue (  DateTime.Now.ToString ('MM')).Selected =true;    
		//Populate year in the dropdownlist
		for(int j = 1900 ;j<=2050;j++)
		{
			ddlYear.Items.Add (new ListItem(j.ToString(),j.ToString () )) ;
		}
		ddlYear.Items.FindByText (DateTime.Now.ToString ('yyyy')).Selected =true;
	}
}

private void ddlMonth_SelectedIndexChanged(object sender, System.EventArgs e)
{
	SetCalendarDate();
}
private void ddlYear_SelectedIndexChanged(object sender, System.EventArgs e)
{
	SetCalendarDate();
}
void SetCalendarDate()
{
	DateTime dtNewDate; 
	dtNewDate =DateTime.Parse (Int16.Parse(ddlMonth.SelectedItem.Text) + '/1/' + Int16.Parse( ddlYear.SelectedItem.Text));
	Calendar1.TodaysDate=dtNewDate ;
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.