ASP.Net and C # - Reducing DropDownList options according to current date

0

On my client's site, I have on one page a DropDownList with the months of the year. Unfortunately, the value property is written in months rather than numbers.

<asp:DropDownList ID="ddlMesReajuste" runat="server" AppendDataBoundItems="true">
    <asp:ListItem Value="" Text="Selecione o mês de reajuste" Selected></asp:ListItem>
    <asp:ListItem Value="Janeiro" Text="Janeiro"></asp:ListItem>
    <asp:ListItem Value="Fevereiro" Text="Fevereiro"></asp:ListItem>
    <asp:ListItem Value="Março" Text="Março"></asp:ListItem>
    <asp:ListItem Value="Abril" Text="Abril"></asp:ListItem>
    <asp:ListItem Value="Maio" Text="Maio"></asp:ListItem>
    <asp:ListItem Value="Junho" Text="Junho"></asp:ListItem>
    <asp:ListItem Value="Julho" Text="Julho"></asp:ListItem>
    <asp:ListItem Value="Agosto" Text="Agosto"></asp:ListItem>
    <asp:ListItem Value="Setembro" Text="Setembro"></asp:ListItem>
    <asp:ListItem Value="Outubro" Text="Outubro"></asp:ListItem>
    <asp:ListItem Value="Novembro" Text="Novembro"></asp:ListItem>
    <asp:ListItem Value="Dezembro" Text="Dezembro"></asp:ListItem>
</asp:DropDownList>

I needed this Dropdown to "go away" with a couple of months of options. For example, today is 21/6. So I should only have access to the options from August onwards. But by the time I get past 10/7, I'd have access to options from September onwards.

That is, the options are always from the next month on (if we are before the 11th of the current month) and two months after the current one for if we are from the 11th until the last day of the current month.

Of course for the month of November, it would only be December if we were before 11/11. And for December, every month would be available, minus the January if we were on 11/1.

I know it sounds kind of complicated, but I was not sure how to do anything about it because it's not possible to put ID in the ListItems and also because the Values of the items are the months written.

What could I do?

    
asked by anonymous 21.06.2018 / 16:53

1 answer

1

From what I understood of your problem, you want me to add 1 month after the current one when the day is less than 11 and add 2 months when the day is greater than 11. I made a code snippet for this situation .

Change your dropdown this way.

<asp:DropDownList ID="ddlMesReajuste" runat="server">
</asp:DropDownList>

Add

public void PopularDropDown()
{
        int quantidadeMesesAdicional = 1;

        if (DateTime.Today.Day > 11)
            quantidadeMesesAdicional = 2;

        int indiceMes = DateTime.Today.Month + quantidadeMesesAdicional;

        //Se o indice for 13 irá voltar para 1 que é Janeiro. 
        //Se o indice for 14 irá voltar para 2 que é Fevereiro.
        if (indiceMes == 13)
            indiceMes = 1;
        else if (indiceMes == 14)
            indiceMes = 2;

        Dictionary<byte, string> dictionaryDatas = new Dictionary<byte, string>();

        dictionaryDatas.Add(1, "Janeiro");
        dictionaryDatas.Add(2, "Fevereiro");
        dictionaryDatas.Add(3, "Março");
        dictionaryDatas.Add(4, "Abril");
        dictionaryDatas.Add(5, "Maio");
        dictionaryDatas.Add(6, "Junho");
        dictionaryDatas.Add(7, "Julho");
        dictionaryDatas.Add(8, "Agosto");
        dictionaryDatas.Add(9, "Setembro");
        dictionaryDatas.Add(10, "Outubro");
        dictionaryDatas.Add(11, "Novembro");
        dictionaryDatas.Add(12, "Dezembro");

        ListItemCollection lista = new ListItemCollection();

        foreach (var data in dictionaryDatas.Where(w => w.Key >= indiceMes))
        {
            lista.Add(new ListItem(data.Value, data.Value));
        }

        ddlMesReajuste.DataSource = lista;
        ddlMesReajuste.DataBind();
}

Finally call this method in your Page_Load within an if (! IsPostBack) block

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopularDropDown();
        }
    }
    
22.06.2018 / 18:54