none
C# Dropdown contains Month need to convert it to a Int RRS feed

  • Question

  • Currently I have a dropdown that has the Months

    January
    ~
    December

    Depending on which  Month they choose I need to convert it to a Number

    this below is just rough thinking, as I can not find this in a Search.

    I was thinking of something like this:

    Public void ChangeMonth(){
    if DD1 = January
    then DD1 = 1

    If DD1 = February 
    then DD1 = 2

    so on so forth for all the months.

    Any ideas on how to get this to work with DD1 ( DD1 = Dropdown)

    this is rough code:

    Dictionary<string, string> months = new Dictionary<string, string>()
    {
                     { "january", "01"},
                     { "february", "02"},
                     { "march", "03"},
                     { "april", "04"},
                     { "may", "05"},
                     { "june", "06"},
                     { "july", "07"},
                     { "august", "08"},
                     { "september", "09"},
                     { "october", "10"},
                     { "november", "11"},
                     { "december", "12"},
    };
    foreach (var month in months)
    {
         if (StringThatContainsMonth.ToLower().Contains(month.Key))
         {
             string thisMonth = month.Value;

                  DD1 = thisMonth;

         }
    }



    • Edited by cowboy2066 Tuesday, September 3, 2019 5:49 PM
    Tuesday, September 3, 2019 4:59 PM

Answers

  • Hi,

    You can build a Dropdown list in Visual Web Part like this:

    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        <asp:ListItem Value="1">January</asp:ListItem>
        <asp:ListItem Value="2">February</asp:ListItem>
        <asp:ListItem Value="3">March</asp:ListItem>
        <asp:ListItem Value="4">April</asp:ListItem>
        <asp:ListItem Value="5">May</asp:ListItem>
        <asp:ListItem Value="6">June</asp:ListItem>
        <asp:ListItem Value="7">July</asp:ListItem>
        <asp:ListItem Value="8">August</asp:ListItem>
        <asp:ListItem Value="9">September</asp:ListItem>
        <asp:ListItem Value="10">October</asp:ListItem>
        <asp:ListItem Value="11">November</asp:ListItem>
        <asp:ListItem Value="12">December</asp:ListItem>
    </asp:DropDownList>
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>

    In code behind, get the Month Number value:

              protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
                {
                    Label1.Text = DropDownList1.SelectedItem.Value;
                }


    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by cowboy2066 Wednesday, September 4, 2019 4:01 PM
    Wednesday, September 4, 2019 7:25 AM

All replies

  • Hi cowboy

    You are going in a right direction but implement as switch case.

    int caseSwitch = January;
          
          switch (caseSwitch)
          {
              case "January":
                  Console.WriteLine("1");
                  break;
              case February:
                  Console.WriteLine("2");
                  break;
              default:
                  Console.WriteLine("Default case");
                  break;
          }

    This will reduce the complexity on code.

    Regards,

    krishna


    Tuesday, September 3, 2019 5:53 PM
  • Krishna

    How do I get it to relate to the Dropdown value? and make the DD1 equal the new case?

    Would it be like this:
    DD1 = January;

    int caseSwitch = DD1;
         
         
    switch (caseSwitch)
         
    {
             
    case "January": DD1("1");
                 
    break;
             
    case "February": DD1("2")
                 
    break;
               case "March": DD1("3")

    default:

    DD1= (Default case");

         
    }

    Do I need this below:

    foreach (var month in months)
    {
         if (StringThatContainsMonth.ToLower().Contains(month.Key))
         {
             string thisMonth = month.Value;

                  DD1 = thisMonth;

         }
    }





    • Edited by cowboy2066 Tuesday, September 3, 2019 6:39 PM
    Tuesday, September 3, 2019 5:57 PM
  • Hi,

    Implement this as key value pair for more efficient way:

    Hashtable ht = new Hashtable();
    
    ht.Add("JAN",1);
    ht.Add("FEB", 2);
        
    int month = (int)ht["FEB"];
    
    Console.WriteLine(month);
    
    O/P: 2

    in your case you will build the hash table and then you will say as 

    int month = (int)ht(DD1)

    Hope this should helps you.

    Regards,

    Krishna

    Tuesday, September 3, 2019 7:02 PM
  • Quick Questions:

    O/P: 2 --- What is this?

    this is throwing an error:
    int month = (int)ht(DD1)

    ht is throwing the error:

    "being used as a 'variable' but is used like a 'method'.

    will the above code work with the  Dropdown  (ddl1) being Selected  and the person chooses March,  as how will the hashtable know what the person selected?

    so the hashtable hold my values, and then the  int Month = (int)ht(ddl1)

    Month will hold my value of 3?




    • Edited by cowboy2066 Tuesday, September 3, 2019 8:05 PM
    Tuesday, September 3, 2019 7:23 PM
  • Hi,

    You can build a Dropdown list in Visual Web Part like this:

    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        <asp:ListItem Value="1">January</asp:ListItem>
        <asp:ListItem Value="2">February</asp:ListItem>
        <asp:ListItem Value="3">March</asp:ListItem>
        <asp:ListItem Value="4">April</asp:ListItem>
        <asp:ListItem Value="5">May</asp:ListItem>
        <asp:ListItem Value="6">June</asp:ListItem>
        <asp:ListItem Value="7">July</asp:ListItem>
        <asp:ListItem Value="8">August</asp:ListItem>
        <asp:ListItem Value="9">September</asp:ListItem>
        <asp:ListItem Value="10">October</asp:ListItem>
        <asp:ListItem Value="11">November</asp:ListItem>
        <asp:ListItem Value="12">December</asp:ListItem>
    </asp:DropDownList>
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>

    In code behind, get the Month Number value:

              protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
                {
                    Label1.Text = DropDownList1.SelectedItem.Value;
                }


    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by cowboy2066 Wednesday, September 4, 2019 4:01 PM
    Wednesday, September 4, 2019 7:25 AM