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

Asked

Viewed 227 times

0

On my client’s website, I have a dropdown list with the months of the year. Unfortunately, the value property has months written instead of 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 "disappear" with some options of months. For example, today is day 21/6. Then I should only have access to the options from August onwards. But when it passes 10/7, then I would have access to the options from September onwards.

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

Of course, for November, you’d only have December if we were before 11/11. And for December, every month would be available, except for January if it was 11/1.

I know it seems a little complicated, but I was wondering how to do something about it because it is not possible to put ID on the Listitems and also because the Values of the items are written months.

What I could do?

1 answer

1


From what I understand 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 longer than 11. I’ve done 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 inside an if block (!Ispostback)

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopularDropDown();
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.