Hi,
Please refer below code.
HTML
<div>
<script type="text/javascript">
function ValidateMonth(sender, args) {
var fromMonth = document.getElementById('ddlFromMonths').value;
var toMonth = document.getElementById('ddlToMonths').value;
args.IsValid = parseInt(toMonth) >= parseInt(fromMonth);
}
</script>
From :
<asp:DropDownList ID="ddlFromMonths" runat="server">
</asp:DropDownList>
To:
<asp:DropDownList ID="ddlToMonths" runat="server">
</asp:DropDownList>
<asp:CustomValidator runat="server" Display="Dynamic" ErrorMessage="To month must be greter than from month"
ClientValidationFunction="ValidateMonth" ForeColor="Red" ValidationGroup="val1"></asp:CustomValidator>
<br />
<asp:Button Text="Submit" runat="server" ValidationGroup="val1" />
</div>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
PopulateMonths(ddlFromMonths);
PopulateMonths(ddlToMonths);
}
}
private void PopulateMonths(DropDownList dropDownList)
{
var months = DateTimeFormatInfo.CurrentInfo.MonthNames.Select((m, i) => new { Month = m, Value = i + 1 }).Take(DateTimeFormatInfo.CurrentInfo.MonthNames.Length - 1);
dropDownList.DataSource = months;
dropDownList.DataTextField = "Month";
dropDownList.DataValueField = "Value";
dropDownList.DataBind();
}
Screenshot
