Link to home
Start Free TrialLog in
Avatar of JeffDrummond
JeffDrummond

asked on

Finding control in datagrid EditItemTemplate

In my C# ASP.Net application, I have a datagrid with item and edititem
templates.  On my datagrid Edit Command, I am unable to find a dropdownlist
control in the edititemtemplate.  Please take a look and let me know what I am
doing wrong!  Thanks.

Code-behind
===========
private void dgRooms_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
      try
      {
            //set the edit item index
            dgRooms.EditItemIndex = e.Item.ItemIndex;

                        
            //fill the drop down lists, one-by-one
            //fill the dataset
            this.RoomSettingsAdapter.Fill(RoomsDataset);

            //create a filter string
            string filter;

            //room location
            DropDownList ddl1 = (DropDownList)e.Item.FindControl("ddlEditRoomLocation");  <==DOES NOT FIND DROPDOWN HERE
            filter ="SettingType = " + 8;
            ddl1.DataSource = this.RoomsDataset.Settings.Select(filter);
            ddl1.DataBind();
                        

      }
      catch (Exception ex)
      {
            Response.Write(ex.Message);
      }
}

HTML CODE
====================

<asp:TemplateColumn HeaderText="Location">
      <ItemTemplate>                                                      
                       <asp:Label id="Label27" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.LocationDescription") %>'></asp:Label>
      </ItemTemplate>
                                                                              <EditItemTemplate>
            <asp:DropDownList id="ddlEditRoomLocation" runat="server" DataTextField="Setting" DataValueField="SettingsIdS">                                  </asp:DropDownList>                                                      
               </EditItemTemplate>
</asp:TemplateColumn>
ASKER CERTIFIED SOLUTION
Avatar of faifai
faifai

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hi try like this it may help you

filter ="SettingType = " + 8;

DropDownList ddl1=  (DropDownList)e.Item.Cells[4].Controls[0];

ddl1.DataSource =  this.RoomsDataset.Settings.Select(filter);

ddl1.DataBind();

Thanks
Avatar of JeffDrummond
JeffDrummond

ASKER

Thanks faifai.  It does work in ItemDataBound.

I still do not know why I could not find the control in the EditCommand?!?!
you may try to add some breakpoint in edit command, itemdatabound_command and page load,  u will know which event will process first.