Friday, 8 April 2011

Populating Years in DropDownList Using Programmatically


Populating Years Programmatically as below .

<div>
     Acadamic Year: &nbsp
    <asp:DropDownList ID="ddAcdYear" runat="server"  AutoPostBack="true"
                 Height="22px"
                Width="150px" >
            </asp:DropDownList>
    </div>

C#

public void GetYears()
    {
        try
        {
            int Year = 2000;
            int fromyear = Year;
            ddAcdYear.Items.Clear();
            ddAcdYear.Items.Add("-Year-");
            for (int j = Year; j <= DateTime.Now.Year; j++)
            {

                ListItem fros = new ListItem(fromyear.ToString() + "-" + (j + 1).ToString());

                ddAcdYear.Items.Add(fros);
                fromyear = j + 1;

            }
        }
        catch (Exception ex)
        {
            
        }
    }


No comments:

Post a Comment