473,772 Members | 3,603 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DropDownList in EditItemTemplat e

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_OnEditCom mand"
OnUpdateCommand ="dgHPU_OnUpdat eCommand"
OnCancelCommand ="dgHPU_OnCance lCommand"
OnItemDataBound ="dgHPU_OnItemD ataBound">
<Columns>
<asp:BoundColum n DataField="HPUI d" Visible="False" ReadOnly="True"/>
<asp:EditComman dColumn ButtonType="Lin kButton" UpdateText="Sav e"
CancelText="Can cel" EditText="Edit" ItemStyle-Width="10%"/>
<asp:TemplateCo lumn HeaderText="Hou r">
<ItemTemplate >
<asp:label id="lblHourPeri od" runat="server" Text='<%#
DataBinder.Eval (Container, "DataItem.HourP eriod") %>'/>
</ItemTemplate>
<EditItemTempla te>
<asp:DropDownLi st ID="cboEditHour Period" Runat="server"
DataSource='<%#
FetchHourPeriod sUpdate((DataBi nder.Eval(Conta iner,
"DataItem.HPUId ")).ToStrin g()) %>'
DataTextField=" HourPeriod" DataValueField= "HourPeriod_Unf "/>
</EditItemTemplat e>
</asp:TemplateCol umn>
</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_OnItemDat aBound(object source,
System.Web.UI.W ebControls.Data GridItemEventAr gs e)
{
if (e.Item.ItemTyp e == ListItemType.Ed itItem)
{
Label lblHourPeriod = ((Label)
e.Item.Cells[2].FindControl("l blHourPeriod")) ;
DropDownList cboEditHourPeri od = ((DropDownList)
e.Item.Cells[2].FindControl("c boEditHourPerio d"));
cboEditHourPeri od.SelectedInde x =
cboEditHourPeri od.Items.IndexO f(cboEditHourPe riod.Items.Find ByText(lblHourP e
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 2666
I answered my own question within minutes, however, I think the solution is
UGLY

protected string sHourPeriod;

protected void dgHPU_OnItemDat aBound(object source,
System.Web.UI.W ebControls.Data GridItemEventAr gs e)
{
if (e.Item.ItemTyp e == ListItemType.Ed itItem)
{
DropDownList cboEditHourPeri od = ((DropDownList)
e.Item.Cells[2].FindControl("c boEditHourPerio d"));
cboEditHourPeri od.SelectedInde x =
cboEditHourPeri od.Items.IndexO f(cboEditHourPe riod.Items.Find ByText(sHourPer i
od));
}
}

protected void dgHPU_OnEditCom mand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
{
sHourPeriod = ((Label)
e.Item.Cells[2].FindControl("l blHourPeriod")) .Text;

dgHPU.EditItemI ndex = e.Item.ItemInde x;
PopulateDataGri d();
}

"George Durzi" <gd****@hotmail .com> wrote in message
news:ep******** ******@tk2msftn gp13.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_OnEditCom mand"
OnUpdateCommand ="dgHPU_OnUpdat eCommand"
OnCancelCommand ="dgHPU_OnCance lCommand"
OnItemDataBound ="dgHPU_OnItemD ataBound">
<Columns>
<asp:BoundColum n DataField="HPUI d" Visible="False" ReadOnly="True"/>
<asp:EditComman dColumn ButtonType="Lin kButton" UpdateText="Sav e"
CancelText="Can cel" EditText="Edit" ItemStyle-Width="10%"/>
<asp:TemplateCo lumn HeaderText="Hou r">
<ItemTemplate >
<asp:label id="lblHourPeri od" runat="server" Text='<%#
DataBinder.Eval (Container, "DataItem.HourP eriod") %>'/>
</ItemTemplate>
<EditItemTempla te>
<asp:DropDownLi st ID="cboEditHour Period" Runat="server"
DataSource='<%#
FetchHourPeriod sUpdate((DataBi nder.Eval(Conta iner,
"DataItem.HPUId ")).ToStrin g()) %>'
DataTextField=" HourPeriod" DataValueField= "HourPeriod_Unf "/>
</EditItemTemplat e>
</asp:TemplateCol umn>
</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_OnItemDat aBound(object source,
System.Web.UI.W ebControls.Data GridItemEventAr gs e)
{
if (e.Item.ItemTyp e == ListItemType.Ed itItem)
{
Label lblHourPeriod = ((Label)
e.Item.Cells[2].FindControl("l blHourPeriod")) ;
DropDownList cboEditHourPeri od = ((DropDownList)
e.Item.Cells[2].FindControl("c boEditHourPerio d"));
cboEditHourPeri od.SelectedInde x =
cboEditHourPeri od.Items.IndexO f(cboEditHourPe riod.Items.Find ByText(lblHourP e 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
2806
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 Description"></asp:BoundColumn> <asp:TemplateColumn runat="server" HeaderText="Id Type Option" "> <itemtemplate> <asp:label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "TypeOption") %>' /> <asp:label runat="server" ID="LlbTypeOption" Visible=False...
2
2238
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. I get the following message for this code but I suspect that the " UltraWebGrid1.Rows.Cells.Value = ddlGeography;" is not how you bind the dropdownlist control to the grid. ERROR: "The type 'System.Web.UI.WebControls.DropDownList' must be...
0
4794
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 TemplateColumn with a DropDownList (DDL) in the FooterItemTemplate that is populated in the Page_Load event using a Sub Routine as the DataSource of the DDL. The FooterItemTemplate's DDL populates as expected when the DataGrid's DataBind method is called. ...
2
2023
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 show the current value in edit mode. I'm sure this is a common question. I've reviewed several related post and tried them out to no avial. Some of the offerings in listgroup look right on, but are not clear on where to handle the suggested...
0
1833
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 it's a Sharepoint WebPart. I'm trying to do something fairly simple, create a datagrid that displays where and when a person works and allows them to change some of the information via DropDownLists. When the user clicks to edit a row, three of...
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 upon SelectedIndexChanged event in the dropdownlist? thank you. -Sergey <ASP:DataGrid id="myDataGrid" runat="server"
2
3106
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 when trying to locate my DropDownList so that I can populate it. It doesn't find the control Here is what I have: asp.net:
1
1875
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 DropDownList is interpreting the default DateTime value as a string value "1/1/0001 12:00:00 AM". DateTime.Parse in turn converts that string value to #12:00:00 AM# with no date values. What am I doing wrong here? Here is some relevant code...
2
7320
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 SelectedIndexChanged event. I also have another TemplateColumn, with a RequiredFieldValidator that validates a TextBox in the column. It all looks like this, in broad strokes:
0
1476
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 edit button, the modified text in the textbox is effectively updated in the table, but instead of updating the chosen value of the dropdownlist, the field in the table becomes empty (old value is updated by nothing). The items of the dropdownlist...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10104
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9912
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8934
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6715
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
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 we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.