DropDownList in EditItemTemplate

Discussion in 'ASP .Net' started by George Durzi, Sep 27, 2004.

  1. George Durzi

    George Durzi Guest

    When my datagrid is in edit mode, one of my columns is edited using a drop
    down list.

    I'm able to bind the DropDownList to a DataSource when in edit mode.
    HOWEVER, I can't preset the DropDownList's value when the Edit button is
    clicked for that row inside the datagrid.

    Here's a simplified version of my datagrid:

    <asp:datagrid id="dgHPU" runat="server"
    OnEditCommand="dgHPU_OnEditCommand"
    OnUpdateCommand="dgHPU_OnUpdateCommand"
    OnCancelCommand="dgHPU_OnCancelCommand"
    OnItemDataBound="dgHPU_OnItemDataBound">
    <Columns>
    <asp:BoundColumn DataField="HPUId" Visible="False" ReadOnly="True"/>
    <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Save"
    CancelText="Cancel" EditText="Edit" ItemStyle-Width="10%"/>
    <asp:TemplateColumn HeaderText="Hour">
    <ItemTemplate>
    <asp:label id="lblHourPeriod" runat="server" Text='<%#
    DataBinder.Eval(Container, "DataItem.HourPeriod") %>'/>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:DropDownList ID="cboEditHourPeriod" Runat="server"
    DataSource='<%#
    FetchHourPeriodsUpdate((DataBinder.Eval(Container,
    "DataItem.HPUId")).ToString()) %>'
    DataTextField="HourPeriod" DataValueField="HourPeriod_Unf"/>
    </EditItemTemplate>
    </asp:TemplateColumn>
    </asp:datagrid>

    I figured that I should preset the DropDownList in the ItemDataBound event
    of the datagrid, so I did this:

    protected virtual void dgHPU_OnItemDataBound(object source,
    System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if (e.Item.ItemType == ListItemType.EditItem)
    {
    Label lblHourPeriod = ((Label)
    e.Item.Cells[2].FindControl("lblHourPeriod"));
    DropDownList cboEditHourPeriod = ((DropDownList)
    e.Item.Cells[2].FindControl("cboEditHourPeriod"));
    cboEditHourPeriod.SelectedIndex =
    cboEditHourPeriod.Items.IndexOf(cboEditHourPeriod.Items.FindByText(lblHourPe
    riod.Text));
    }
    }

    However, the FindControl call returns nothing, so then I obviously get an
    Object reference not set to an instance of an object. error when I try to
    set the SelectedIndex.

    Anything I'm missing?
     
    George Durzi, Sep 27, 2004
    #1
    1. Advertisements

  2. George Durzi

    George Durzi Guest

    I answered my own question within minutes, however, I think the solution is
    UGLY

    protected string sHourPeriod;

    protected void dgHPU_OnItemDataBound(object source,
    System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if (e.Item.ItemType == ListItemType.EditItem)
    {
    DropDownList cboEditHourPeriod = ((DropDownList)
    e.Item.Cells[2].FindControl("cboEditHourPeriod"));
    cboEditHourPeriod.SelectedIndex =
    cboEditHourPeriod.Items.IndexOf(cboEditHourPeriod.Items.FindByText(sHourPeri
    od));
    }
    }

    protected void dgHPU_OnEditCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    sHourPeriod = ((Label)
    e.Item.Cells[2].FindControl("lblHourPeriod")).Text;

    dgHPU.EditItemIndex = e.Item.ItemIndex;
    PopulateDataGrid();
    }
     
    George Durzi, Sep 27, 2004
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.