1

I have a DropDownList in a GridView EditItemTemplate. The ddl is to be populated at runtime; options in each row may vary.

The GridView is working as desired. In non-edit mode, it binds to data. In edit mode the controls specified in the EditItemTemplate are rendered.

Question: Why can't I FindControl on this DropDownList??? Note that I can FindControl on a Calendar control that's also in an EditItemTemplate.

Below is the ASPX & C# code.

Thanks!!

<asp:UpdatePanel ID="UpdatePanelSelections" runat="server">
<ContentTemplate>
    <asp:GridView ID="GridViewSelections" runat="server" 
        ...

        <Columns>

            <asp:TemplateField HeaderText="Options" >
                <ItemTemplate>
                    <asp:Label ID="CurrentOption" runat="server" 
                        Text='<%# Eval("Options.OptionName") %>'>
                    </asp:Label>
                </ItemTemplate>

                <EditItemTemplate>
                    <asp:DropDownList ID="ddlOptions" runat="server" >
                    </asp:DropDownList>
                </EditItemTemplate>
                <HeaderStyle HorizontalAlign="Left" />
            </asp:TemplateField>


            <asp:TemplateField HeaderText="Date" >
                <ItemTemplate>
                    <asp:Label ID="CurrentlySelectedDate" runat="server" 
                        Text='<%# Eval("SomeDate") %>'>
                    </asp:Label>
                </ItemTemplate>

                <EditItemTemplate>
                    <asp:Calendar ID="calNewDate" runat="server" SelectedDate='<%# Bind("SomeDate") %>' VisibleDate='<%# Bind("SomeDate") %>' />
                </EditItemTemplate>
                <HeaderStyle HorizontalAlign="Left" />
            </asp:TemplateField>

        ...


    protected void GridViewSelections_RowEditing(object sender, GridViewEditEventArgs e)
   {
    // FindControl calNewDate works in GridViewSelections_RowUpdating but not in GridViewSelections_RowEditing
    Calendar calNewDate = GridViewSelectionss.Rows[e.NewEditIndex].FindControl("calNewDate") as Calendar;

    // FindControl ddlOptions doesn't work anywhere!
    DropDownList ddlOptions = GridViewSelections.Rows[e.NewEditIndex].FindControl("ddlOptions") as DropDownList;

// Looking in Cells doesn't work either
DropDownList ddlOptions2 = GridViewSelections.Rows[e.NewEditIndex].Cells[2].FindControl("ddlOptions") as DropDownList;
TheGeekYouNeed
  • 7,509
  • 2
  • 26
  • 43
Mark Maslar
  • 1,121
  • 4
  • 16
  • 28
  • at what point do you execute the FindControl code, might be before the gridview has generated the rows? – Kris Ivanov Feb 03 '11 at 20:17
  • I'm firing it in RowEditing. The intent is that I'd grab the options for that particular row & populate the ddl. – Mark Maslar Feb 03 '11 at 20:28
  • try doing it in the binding event, I don't think that row is created yet, only the index – Kris Ivanov Feb 03 '11 at 20:35
  • 1
    check for DataControlRowState.Edit in the databound even and that is the row you want to do the findcontrol – Kris Ivanov Feb 03 '11 at 20:47
  • In _RowDataBound, if (e.Row.RowType == DataControlRowType.DataRow) { GridViewSelections.Rows.Count //Count is 0 here – Mark Maslar Feb 03 '11 at 20:47
  • if ((e.Row.RowType == DataControlRowType.DataRow) && (e.Row.RowState.HasFlag(DataControlRowState.Edit))) { //The Row Count is 1 here. e.Row.RowIndex==1, but trying to access that GridView row blows up -- index out of range. – Mark Maslar Feb 03 '11 at 21:08

1 Answers1

1

The solution turned out to be checking that the DataItem isn't null. Just checking for RowType and Edit flag wasn't enough.

protected void GridViewSelections_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if ((e.Row.RowType == DataControlRowType.DataRow) && (e.Row.RowState.HasFlag(DataControlRowState.Edit) && (e.Row.DataItem != null)))
    {
        DropDownList ddlOptions = e.Row.FindControl("ddlOptions") as DropDownList;
        ddlOptions.Items.Add(new ListItem("aaa", "1"));
        ddlOptions.Items.Add(new ListItem("bbb", "2"));
        ddlOptions.Items.Add(new ListItem("ccc", "3"));
Mark Maslar
  • 1,121
  • 4
  • 16
  • 28