locked
Loading all my dropdowlist from one objectdatasource RRS feed

  • Question

  • User360451555 posted

    Hi Folks how u doin,

    well am facing a challange down here.

    I have a series of dropdownlist all over my site and there are a couple of them that share the same data. Unfortunately this data aint coming from the database. So i would like to make one central point where i can bind all datasources of Dropdownlists.

    A Clear example i have is that i have dropdownlists that list Academic years 2009/2010, 2010/2011, 2011, 2012...

    So i want to make a sort of arraylist, load these items there and put them in some kind of a method that i can call during page load and bind it onto any of my Academic year dropdownlists.

    Someone told me that i could also make use of an ObjectDataSource but really dont know how to do about it. Please u gotta help me on this folks


    Sunday, December 12, 2010 11:50 PM

Answers

  • User360451555 posted

    I finally got the answer.

    I wrote a Method in my class as follows

    public List<string> GetAllAY()
    {
         List<string> myList = new List<string>();
            myList.Add("2009/2010");
            myList.Add("2010/2011");
            myList.Add("2011/2012");
            myList.Add("2012/2013");
            myList.Add("2013/2014");
            myList.Add("2014/2015");
            myList.Add("2015/2016");
    
            return myList;
     }


    Then in my ASPX page CS file, i do it like this

    CommonRoutines Routine = new CommonRoutines();
    
    //**********************************************
    
    ddlYear.DataSource = Routine.GetAllAY();
    ddlYear.DataBind();

    Thanks yo'l folks for your contribution....but i got it finally right

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 16, 2010 5:05 PM

All replies

  • User-1923420989 posted

    Define a class with required properties , and create generic collection for this class, and add the items into the collection by some business logic method which will return business object collection.

    now you can use object data source's data source as your collection and select method is that business logic method.

    you can bind your object data source with your number of dropdownlists...

    got it ?

    Monday, December 13, 2010 1:40 AM
  • User1224194097 posted

    You can create a class for DropDownList databinding purpose and call the method in page Load or desired event handler

    public static class DropDownListHelper
    {
        public static void LoadAcademicDropDownList(DropDownList DropDownListToLoad)
        {
            try
            {
                DropDownListToLoad.DataSource = new[] { "2010/2011""2011""2012""2012/2013" };
                DropDownListToLoad.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

    Now, you can bind the DDL as

    DropDownListHelper.LoadAcademicDropDownList(DropDownList1);
    DropDownListHelper.LoadAcademicDropDownList(DropDownList2);
    Monday, December 13, 2010 1:56 AM
  • User360451555 posted

    Hey....thnx man for the teng.....Well i tried to implement the class as you had told me but then i get another error.

    I tried to put the teng into my page load but more errors were created. Look at how i mande it

    public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DropDownListHelper.LoadAcademicDropDownList(DropDownList1);
        }
    }


    Monday, December 13, 2010 4:44 AM
  • User1716267170 posted

    Since the data doesn't come from database, you can just return the expected list/array in select method of objectdatasource. The you can bind objectdatasource to each dropdownlists. But note, since the data doesn't change for each dropdownlist, you could also consider to cache the data in select method if it's not got directly. Thanks. 

    Thursday, December 16, 2010 12:49 AM
  • User360451555 posted

    I finally got the answer.

    I wrote a Method in my class as follows

    public List<string> GetAllAY()
    {
         List<string> myList = new List<string>();
            myList.Add("2009/2010");
            myList.Add("2010/2011");
            myList.Add("2011/2012");
            myList.Add("2012/2013");
            myList.Add("2013/2014");
            myList.Add("2014/2015");
            myList.Add("2015/2016");
    
            return myList;
     }


    Then in my ASPX page CS file, i do it like this

    CommonRoutines Routine = new CommonRoutines();
    
    //**********************************************
    
    ddlYear.DataSource = Routine.GetAllAY();
    ddlYear.DataBind();

    Thanks yo'l folks for your contribution....but i got it finally right

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 16, 2010 5:05 PM