Hi Vincenzo67,
Refer below example.
HTML
<asp:DropDownList ID="ddlMonths" runat="server">
<asp:ListItem Text="Select" Value=""></asp:ListItem>
<asp:ListItem Text="Jan" Value="1"></asp:ListItem>
<asp:ListItem Text="Feb" Value="2"></asp:ListItem>
<asp:ListItem Text="Mar" Value="3"></asp:ListItem>
<asp:ListItem Text="Apr" Value="4"></asp:ListItem>
<asp:ListItem Text="May" Value="5"></asp:ListItem>
<asp:ListItem Text="Jun" Value="6"></asp:ListItem>
<asp:ListItem Text="Jul" Value="7"></asp:ListItem>
<asp:ListItem Text="Aug" Value="8"></asp:ListItem>
<asp:ListItem Text="Sep" Value="9"></asp:ListItem>
<asp:ListItem Text="Oct" Value="10"></asp:ListItem>
<asp:ListItem Text="Nov" Value="11"></asp:ListItem>
<asp:ListItem Text="Dec" Value="12"></asp:ListItem>
</asp:DropDownList>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
DateTime dateTime = new DateTime(2025, 01, 10);
// List of Months to be display.
List<ListItem> months = new List<ListItem>();
for (int i = 1; i <= 3; i++)
{
months.Add(ddlMonths.Items.Cast<ListItem>()
.Where(x => x.Value.Equals(dateTime.AddMonths(-i).Month.ToString()))
.FirstOrDefault());
}
for (int i = ddlMonths.Items.Count; i > 1; i--)
{
if (!months.Contains(ddlMonths.Items[i - 1]))
{
// Remove items from DropDownList.
ddlMonths.Items.Remove(ddlMonths.Items[i - 1]);
}
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim dateTime As DateTime = New DateTime(2025, 1, 10)
'//' List of Months to be display.
Dim months As List(Of ListItem) = New List(Of ListItem)()
For i As Integer = 1 To 3
months.Add(ddlMonths.Items.Cast(Of ListItem)().Where(Function(x)
Return x.Value.Equals(dateTime.AddMonths(-i).Month.ToString())
End Function).FirstOrDefault())
Next
For i As Integer = ddlMonths.Items.Count To 1 + 1 Step -1
If Not months.Contains(ddlMonths.Items(i - 1)) Then
' Remove items from DropDownList.
ddlMonths.Items.Remove(ddlMonths.Items(i - 1))
End If
Next
End Sub
Screenshot