G
George Durzi
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>
<aspropDownList 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?
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>
<aspropDownList 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?