DropDown List for yearwise months

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Soniad
    New Member
    • Jan 2009
    • 66

    DropDown List for yearwise months

    Here I have created simple dropdown list that shows months for every year.
    U can initialize the year from where u want to start.
    I have started from year "1999".

    Similarly,
    U can use Following example to display list of animals in certain category OR List Of items in certain Product,Etc.

    Here is the Code :
    Code:
    	<%
    	Dim StartYear,TillYear,StrCheck
    	StartYear = 1999
    	TillYear = Year(Date())
    	StrCheck = ""
    
    	Dim MonthsArr(11)
    	MonthsArr(0) = "Jan"
    	MonthsArr(1) = "Feb"
    	MonthsArr(2) = "Mar"
    	MonthsArr(3) = "Apr"
    	MonthsArr(4) = "May"
    	MonthsArr(5) = "Jun"
    	MonthsArr(6) = "Jul"
    	MonthsArr(7) = "Aug"
    	MonthsArr(8) = "Sep"
    	MonthsArr(9) = "Oct"
    	MonthsArr(10) = "Nov"
    	MonthsArr(11) = "Dec"
    	
    	Dim i,StrValue			
    	%>
    	<select name="CmbMonYear">
    	<option value="0">--Select--</option>
    	<%
    	For Yrs = StartYear To TillYear
    		If StrCheck <> Yrs Then
    	%>
    	<optgroup label="<%=Yrs%>">
    		<%End If
    		 For i = 0 To Ubound(MonthsArr)
    		%>
    	<option value="<%=MonthsArr(i)%>"<%If MonthsArr(i) = Request.Form("CmbMonYear") Then 	Response.Write("Selected") End If%>>
    	<%=MonthsArr(i)%></option>
    	<%	Next
    	StrCheck = Yrs
    	Next
    	%>
    	</optgroup>
    	</select>
    Explanation :

    Step 1 : Declare and initialize variables.
    Initialize Start year whatever u want and EndYear.
    Here I have taken started from 1999 to current year.

    Code:
    	<%
    	Dim StartYear,TillYear,StrCheck
    	StartYear = 1999
    	TillYear = Year(Date())
    	StrCheck = ""
        %>
    Step 2 : Declare and Create array for months.

    Code:
    	
    	<%	
    	Dim MonthsArr(11)
    	MonthsArr(0) = "Jan"
    	MonthsArr(1) = "Feb"
    	MonthsArr(2) = "Mar"
    	MonthsArr(3) = "Apr"
    	MonthsArr(4) = "May"
    	MonthsArr(5) = "Jun"
    	MonthsArr(6) = "Jul"
    	MonthsArr(7) = "Aug"
    	MonthsArr(8) = "Sep"
    	MonthsArr(9) = "Oct"
    	MonthsArr(10) = "Nov"
    	MonthsArr(11) = "Dec"
    	
    	Dim i,StrValue			
    	%>
    Step 3 : Start Select list with optiongroup and with first value "--Select--"

    Code:
    	
    	<select name="CmbMonYear">
    	<option value="0">--Select--</option>
    Step 4 : Loop through Start Year and End Year
    Check the year already displayed or not. if not then display else skip and display months.
    Display years in "optgroup"

    Code:
    	<%
    	For Yrs = StartYear To TillYear
    		If StrCheck <> Yrs Then
    	%>
    	<optgroup label="<%=Yrs%>">
    		<%End If%>
    Step 5 : Loop Through Array of months and display months

    Code:
    <%		 For i = 0 To Ubound(MonthsArr)
    		%>
    	<option value="<%=MonthsArr(i)%>"<%If MonthsArr(i) = Request.Form("CmbMonYear") Then 	Response.Write("Selected") End If%>>
    	<%=MonthsArr(i)%></option>
    Step 6 : End Inner for loop and assign the year in a variable

    Code:
    	<%	Next
    	StrCheck = Yrs
    	%>
    Step 7 : End Outer for loop

    Code:
    	
    	<%Next
    	%>
    	</optgroup>
    	</select>
Working...