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. Advertising

  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" <> wrote in message
    news:...
    > 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
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Tkg=?=
    Replies:
    2
    Views:
    29,322
    =?Utf-8?B?Tkg=?=
    Mar 27, 2006
  2. WT
    Replies:
    2
    Views:
    4,573
  3. =?Utf-8?B?ZGVuIDIwMDU=?=

    No Value: DropDownlist in EditItemTemplate Gridview Problem

    =?Utf-8?B?ZGVuIDIwMDU=?=, Oct 11, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    538
    =?Utf-8?B?ZGVuIDIwMDU=?=
    Oct 11, 2006
  4. Steve Hershoff
    Replies:
    2
    Views:
    822
    Steve Hershoff
    Apr 11, 2007
  5. Replies:
    1
    Views:
    377
Loading...

Share This Page