I am trying to bind data to a DropDownList
that I created using the <asp:DropDownList
tag. It successfully works for the first DropDownList
, but an exception is thrown for the remaining two. I know that the data source is not null and does indeed have values, so the only thing I can think of is that the other two list boxes have not yet been loaded or bound.
I attempted to use the Page_LoadComplete
method to add the content, but it doesn't seem to be firing. I'm used to MVC and I'm very new to Web Forms, so I'm not sure what I am doing wrong. Am I correct by stating that the elements have not yet been loaded, and if so how do I bind them once they are loaded?
protected void Page_Load(object sender, EventArgs e)
{
//Page.LoadComplete += new EventHandler(Page_LoadComplete);
if (DataContext == null)
{
DataContext = new ReuseLibraryDataContext(SPContext.Current.Web.Url);
}
// top level category for all documents
_functions = DataContext.ReuseLibrary.Select(p => p.FunctionalCategories).Distinct().ToList();
// the first sub category
_sc1 = DataContext.ReuseLibrary.Select(p => p.SubCategoriesLevel1).Distinct().ToList();
// the second sub category
_sc2 = DataContext.ReuseLibrary.Select(p => p.SubCategoriesLevel2).Distinct().ToList();
// add the functions to the dropdown
listFunctions.DataSource = _functions;
listFunctions.DataBind();
// add the sub cat 1 to the dropdown
listSC1.DataSource = _sc1;
listSC1.DataBind();
// add the sub cat 2 to the dropdown
listSC2.DataSource = _sc2;
listSC2.DataBind();
}
//protected void Page_LoadComplete(object sender, EventArgs e)
//{
// // add the functions to the dropdown
// listFunctions.DataSource = _functions;
// listFunctions.DataBind();
// // add the sub cat 1 to the dropdown
// listSC1.DataSource = _sc1;
// listSC1.DataBind();
// // add the sub cat 2 to the dropdown
// listSC2.DataSource = _sc2;
// listSC2.DataBind();
//}