Hi comunidadmexi,
Please refer below Sample.
HTML
Month Names:
<asp:DropDownList ID="ddlMonthNames" runat="server" AutoPostBack="true" OnSelectedIndexChanged="OnSelectedIndexChanged">
<asp:ListItem Value="" Selected="True"></asp:ListItem>
</asp:DropDownList>
Namespace
C#
using System.Globalization;
VB.Net
Imports System.Globalization
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
mtmonthbind();
}
}
private void mtmonthbind()
{
DateTimeFormatInfo info = DateTimeFormatInfo.GetInstance(null);
int currentMonth = DateTime.Now.Month;
for (int i = 1; i < 13; i++)
{
bool isMonthInPast = ((i + 1) < currentMonth) || (i + 1 == currentMonth && DateTime.Now.Day > 20);
if (!isMonthInPast)
ddlMonthNames.Items.Add(new ListItem(info.GetMonthName(i), i.ToString()));
}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
int currentMonth = DateTime.Now.Month;
if (currentMonth == int.Parse(ddlMonthNames.SelectedValue))
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You can not select current month.');", true);
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
mtmonthbind()
End If
End Sub
Private Sub mtmonthbind()
Dim info As DateTimeFormatInfo = DateTimeFormatInfo.GetInstance(Nothing)
Dim currentMonth As Integer = DateTime.Now.Month
For i As Integer = 1 To 13 - 1
Dim isMonthInPast As Boolean = ((i + 1) < currentMonth) OrElse (i + 1 = currentMonth AndAlso DateTime.Now.Day > 20)
If Not isMonthInPast Then ddlMonthNames.Items.Add(New ListItem(info.GetMonthName(i), i.ToString()))
Next
End Sub
Protected Sub OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim currentMonth As Integer = DateTime.Now.Month
If currentMonth = Integer.Parse(ddlMonthNames.SelectedValue) Then
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('You can not select current month.');", True)
End If
End Sub
Screenshot