I have combo box for month that will load all the previous months until the current month. For example current month today is July. So it'll load from January until July using VB.Net. Here is my combobox in asp.net :
<dx:ASPxComboBox ID="cmbMonth" runat="server" AutoPostBack="true"></dx:ASPxComboBox>
CodePudding user response:
So, assuming this drop down list?
<asp:DropDownList ID="DropDownList1" runat="server"
DataValueField ="value"
DataTextField="text"
AutoPostBack="true" Width="129px" >
</asp:DropDownList>
Then our code to fill above will be this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim CurrentMonth = Month(DateTime.Now)
For i = 1 To CurrentMonth
Dim MyItem As New ListItem(MonthName(i), i)
DropDownList1.Items.Add(MyItem)
Next
' optional please select (quite much required with auto post back = true
DropDownList1.Items.Insert(0, New ListItem("Select Month", "0"))
End If
End Sub
Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDownList1.SelectedIndexChanged
Debug.Print("drop month number = " & DropDownList1.SelectedItem.Value)
Debug.Print("drop month as text = " & DropDownList1.SelectedItem.Text)
End Sub
And the results are this:
when we select say in above (April), then debug shows this:
CodePudding user response:
You need to play with code instead ask silly questions, here i give sample code which get all months in one list and get current month name also. Loop the month list and check that month is current month or not.
Dim months = System.Globalization.DateTimeFormatInfo.InvariantInfo.MonthNames.Take(12).ToList
Dim iMonth As Integer = Month(System.DateTime.Now)
Dim GetMonthName As String = MonthName(iMonth)
For i = 0 To months.Count-1
Dim aa As String = months(i) ' Here u will add months(i) in Combobox one by one
If months(i) = GetMonthName Then
GoTo Reachhear
End If
Next
Reachhear: