Hi there,
In the aspx page I have two drop down lists
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:DropDownList ID="ddlYears" runat="server">
                <asp:ListItem Text="Select" Value=""></asp:ListItem>
                <asp:ListItem Text="2025" Value="2025"></asp:ListItem>
                <asp:ListItem Text="2024" Value="2024"></asp:ListItem>
            </asp:DropDownList>
        </div>
        <br />
        <div>
            <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>
        </div>
    </form>
</body>
I have to warn the user when he selects a year month value that is not yet present.
For example if today I select year 2025 and month June I have to warn that the selection is invalid.
How can I do this? My code below
protected void Page_Load(object sender, EventArgs e)
{
    DateTime dateTime = DateTime.Now;
 
    // List of Months to be display.
    List<ListItem> months = new List<ListItem>();
    for (int i = 1; i <= 8; i++)
    {
        months.Add(ddlMonths.Items.Cast<ListItem>()
                            .Where(x => x.Value.Equals(dateTime.AddMonths(-i).Month.ToString()))
                            .FirstOrDefault());
    }
 
    //Determines first item i.e.Equals Default Item.
    ListItem defaultItem = ddlMonths.Items[0];
 
    //Clears all items in the DropDownList.
    ddlMonths.Items.Clear();
 
    //Reverse the List of last two months.
    months.Reverse();
 
    //Inserts Default Item.
    months.Insert(0, defaultItem);
 
    //Assigning month to DropDownList.
    ddlMonths.DataSource = months;
    ddlMonths.DataBind();
}