Dropdownlist from database display in EditTemplate Gridview Problem

Last post 08-29-2008 1:49 PM by mellamokb. 1 replies.

Sort Posts:

  • Dropdownlist from database display in EditTemplate Gridview Problem

    08-29-2008, 12:11 PM
    • Member
      point Member
    • juliusbacosa
    • Member since 05-08-2008, 5:05 PM
    • 6888 Washington Pio Del Pilar, Makati City
    • Posts 27

     

     why is it that

    string submenuList = ((DropDownList)SubMenuGrid.Rows[e.RowIndex].Controls[e.RowIndex].FindControl("menuContainerList")).SelectedItem.Value;
     Produces an object reference error... 
    I populated the control using... this code
      
       DataTable menuContainers = BrochureAccess.getContainerMenus();
    for (int i = 0; i < menuContainers.Rows.Count; i++)
    {
    ((DropDownList)SubMenuGrid.Rows[e.NewEditIndex].FindControl("menuContainerList")).Items.Add(new ListItem(menuContainers.Rows[i]["menu_TITLE"].ToString(), menuContainers.Rows[i]["menu_ID"].ToString()));
    }
     
     
    If i hardcoded the list in .aspx everything goes fine... but I need to populate the dropdown list from database ...
     
    could somebody help me.... 
    keep on coming gold

    May the force be with us
    http://www.luckstory.com
  • Re: Dropdownlist from database display in EditTemplate Gridview Problem

    08-29-2008, 1:49 PM
    Answer
    • Contributor
      3,561 point Contributor
    • mellamokb
    • Member since 02-09-2007, 5:44 PM
    • Posts 644

    Hello,

    When are you populating and when are you accessing the DropDownList?  When you specify the list statically in the aspx, the ASP.Net Framework engine takes care of making sure those items are available when you need to reference them.  But when you are adding entires to the DropDownList programmatically, the order of events is important.

    Another option that comes to mind would be to let aspx do the work by using an ObjectDataSource: 

    <asp:ObjectDataSource ID="BrochureData" runat="server"
        TypeName="BrochureAccess" SelectMethod="getContainerMenus">
    </asp:ObjectDataSource>
    
    <asp:DropDownList ID="BrochureDropDown" runat="server"
        DataSourceID="BrochureData" DataTextField ="menu_TITLE"
        DataValueField="menu_ID"></asp:DropDownList>

    Then you don't have to worry about any event ordering or object reference issues from the DropDownList.

    Cheers,

    ~ mellamokb

Page 1 of 1 (2 items)