473,511 Members | 10,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DropDownList in EditItemTemplate

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?
Nov 18 '05 #1
1 2649
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" <gd****@hotmail.com> wrote in message
news:ep**************@tk2msftngp13.phx.gbl...
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?

Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2761
by: Stanley J Mroczek | last post by:
How do you load a dropdownlist when edit is clicked in a datagrid ? <Columns> <asp:BoundColumn DataField="OptionDescription" ItemStyle-Wrap="True" HeaderText="Option...
2
2224
by: rmorvay | last post by:
I am trying to dynamically build a dropdownlist and bind it to a cell in a grid. I tried to utilize the following code but I am stuck at the point where I bind the dropdownlist to the grid cell. ...
0
4780
by: Shane O. Pinnell | last post by:
I am sure this has come up before, but I haven't been able to find an answer as of yet. That said, any help is definitely appreciated! I have a datagrid populated from a dataset. I have a...
2
1995
by: jason | last post by:
Pardon my ignorance on this. The below code works, except, when I edit a record and update the two drop downs take the first entry in the dropdownlist if not selected. I'd also like the dropdown to...
0
1812
by: Daniel Doyle | last post by:
Hello and apologies in advance for the amount of code in this post. I've also sent this message to the Sharepoint group, but thought that ASP.NET developers may also be able to help, even though...
1
578
by: sergeyr3 | last post by:
Hey guys, I am new here, so i hope this works out: I have a datagrid which I populate with data from XML file. In EditItemTemplate I have a dropdownlist. How do I fire myDataGrid_UpdateCommand...
2
3095
by: Jeff User | last post by:
Hi C# .NET1.1 I have read Visual Studio help and several articles on the web. Obviously this is a common topic. However, I see this code over and over yet I am stuck with the FindControl method...
1
1853
by: Vagabond Software | last post by:
I'm using a DropDownList in an EditItemTemplate column. This DropDownList will only ever have two dates in it; Now and the default DateTime value (1/1/1900 12:00:00 AM). Somewhere, somehow, the...
2
7304
by: Steve Hershoff | last post by:
Hi everyone, I have a DataGrid with several TemplateColumns. One of these columns has an EditItemTemplate that contains an ASP.Net DropDownList. I'm catching this DropDownList's...
0
1458
by: Dan | last post by:
Hi, I have a detailsview with two fields: in editmode, one is a textbox and the other is a dropdownlist. i want to update both fields using the detailsview. My problem: when clicking on the...
0
7251
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7430
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5673
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3217
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1581
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
790
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
451
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.