• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 680
  • Last Modified:

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>
0
JeffDrummond
Asked:
JeffDrummond
  • 2
1 Solution
 
faifaiCommented:
i think you should add ItemDataBound for your datagrid and do it inside

private void your_datagrid_ItemDataBound (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
if(e.Item.ItemType != ListItemType.EditItem)
      return;
//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");
          filter ="SettingType = " + 8;
          ddl1.DataSource = this.RoomsDataset.Settings.Select(filter);
          ddl1.DataBind();

}
and for your EditCommand should do something like this.
private void your_datagrid_EditCommand (object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
                  this.your_datagrid.EditItemIndex = e.Item.ItemIndex;
                  this.bindyour_datagrid_function();
}
0
 
SystemExpertCommented:
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
0
 
JeffDrummondAuthor Commented:
Thanks faifai.  It does work in ItemDataBound.

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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now