Wednesday, April 4, 2012

Asp.Net - Populate Hours & Min Dropdowns the Quick Way



In Asp.Net we have come across situations where we need to populate the Hrs dropdowns with values 00..23 and Min dropdown with values 00..59.

Once obvious and straignt forwad option is to add these as static list items to the .aspx page, but this will add unnecessary lines of code to the .aspx page, also what if we have to populate more numbers of Hrs and Min dropdowns in the same page, we will end up adding these list items again and again to the same page, this will shoot up the code size drastically. So what do we do to prevent this.

Here's a solution
Add a loop to your codebehind file .vb / .cs file polulate the values 00..23 /   00..59 in a DataTable and use the same to bind it to the DropDown. Here's the code

        // Function to generate a Hrs DataTable
        public static DataTable GetHrsTable()
        {
            DataTable dtHrs = new DataTable();
            DataRow dr;
            dtHrs.Columns.Add("Hrs", typeof(string));
            //
            dr = dtHrs.NewRow();
            dr["Hrs"] = "HH";
            dtHrs.Rows.Add(dr);
            //
            for (int i = 0; i <= 23; i++)
            {
                dr = dtHrs.NewRow();
                dr["Hrs"] = i.ToString("D2");
                dtHrs.Rows.Add(dr);
            }
            dtHrs.AcceptChanges();
            return dtHrs;
        }

        // Function to generate a Min DataTable
        public static DataTable GetMinTable()
        {
            DataTable dtMin = new DataTable();
            DataRow dr;
            dtMin.Columns.Add("Min", typeof(string));
            //
            dr = dtMin.NewRow();
            dr["Min"] = "MM";
            dtMin.Rows.Add(dr);
            //
            for (int i = 0; i <= 59; i++)
            {
                dr = dtMin.NewRow();
                dr["Min"] = i.ToString("D2");
                dtMin.Rows.Add(dr);
            }
            dtMin.AcceptChanges();
            return dtMin;
        }

Notice that while adding the int value 1 to the DataTable we are using a string formatter

ToString("D2");

This will make sure that all the values in the DropDown will have atleast 2 digits, for example 1 will be represented as 01, 5 as 05, and 15 as 15

Now we have the functions generating the DataTables in place, we now need to bind these to the actual dropdowns to see the output, let's do it.

    DataTable dtHrs = GetHrsTable();
    DataTable dtMin = GetMinTable();
    //
    drpStartDateHH.DataSource = dtHrs;
    drpStartDateHH.DataTextField = "Hrs";
    drpStartDateHH.DataValueField = "Hrs";
    drpStartDateHH.DataBind();
    //
    drpStartDateMM.DataSource = dtMin;
    drpStartDateMM.DataTextField = "Min";
    drpStartDateMM.DataValueField = "Min";
    drpStartDateMM.DataBind();

Make sure that you add this code inside the if (!Page.IsPostBack) condition, so that the value in the dropdown do not get duplicated during postbacks.

That's it we have populated the Hrs & Min dropdowns the quick way





Search Flipkart Products:
Flipkart.com

1 comment:

Anonymous said...

good one!