473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Need help with FindControl in DataGrid


I've got a datagrid set up to display data. I've also got an
Edit,Update,Cancel column set up to allow editing of data.

I've got a DropDownList (ID="ddl3")in the EditItemTemplate for a certain
column that I need to populate while in Edit Mode.

Two questions: Is there a way to directly use another DropDownList as a
DataSource? If so how?

I'm using the following code to try to populate the DropDownList, but
getting an "Object Reference not set to an Instance of an Object" error
when trying to add items to it. The column is the 4th column (index=3).

If I remove the code after the "LoadGrid()" line, then everything
populates fine except for the DropDownList, which is empty.

Hopefully someone can help me get this code working.

-----------------------------------------
Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles
DataGrid1.EditCommand
DataGrid1.EditItemIndex = e.Item.ItemIndex
btnAddServer.Visible = False
LoadGrid()
Dim ddl As DropDownList =
CType(e.Item.Cells(3).FindControl("ddl3"), DropDownList)
For i As Integer = 0 To ddlTechs.Items.Count - 1
ddl.Items.Add(ddlTechs.Items.Item(i).Value)
Next
End Sub
----------------------------------------

*** Sent via Developersdex http://www.developersdex.com ***
Nov 19 '05 #1
10 1871

Hi Terry:

One tip I can offer is to use FindControl on the Item instead of on a
Cell. This allows the column to move and you don't need to change your
code, i.e. e.Item.FindControl("..."). I have a little article with
more info: http://odetocode.com/Articles/116.aspx

Also, you might want to step through with the debugger and find out
exactly what variable is undefined (Nothing).

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Sun, 01 May 2005 18:56:27 -0700, Terry Olsen <to******@hotmail.com>
wrote:

I've got a datagrid set up to display data. I've also got an
Edit,Update,Cancel column set up to allow editing of data.

I've got a DropDownList (ID="ddl3")in the EditItemTemplate for a certain
column that I need to populate while in Edit Mode.

Two questions: Is there a way to directly use another DropDownList as a
DataSource? If so how?

I'm using the following code to try to populate the DropDownList, but
getting an "Object Reference not set to an Instance of an Object" error
when trying to add items to it. The column is the 4th column (index=3).

If I remove the code after the "LoadGrid()" line, then everything
populates fine except for the DropDownList, which is empty.

Hopefully someone can help me get this code working.

-----------------------------------------
Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArg s) Handles
DataGrid1.EditCommand
DataGrid1.EditItemIndex = e.Item.ItemIndex
btnAddServer.Visible = False
LoadGrid()
Dim ddl As DropDownList =
CType(e.Item.Cells(3).FindControl("ddl3"), DropDownList)
For i As Integer = 0 To ddlTechs.Items.Count - 1
ddl.Items.Add(ddlTechs.Items.Item(i).Value)
Next
End Sub
----------------------------------------

*** Sent via Developersdex http://www.developersdex.com ***


Nov 19 '05 #2
Thanks for the tip. I'll check it out and see what I can do.

I moved the code over to the Update Event and it can find the control
just fine. Does the control not exist until AFTER the DataGrid is
rendered and we exit the Edit Event Handler? If so, how would I
populate it so that the user can select an item from it before clicking
the Update button?

*** Sent via Developersdex http://www.developersdex.com ***
Nov 19 '05 #3
Hi Terry:

Since you are pulling items from another drop down list it would
depend on the order of events and when the items appear in the other
list. Are you using databinding to populate ddlTechs?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 02 May 2005 08:53:26 -0700, Terry Olsen <to******@hotmail.com>
wrote:
Thanks for the tip. I'll check it out and see what I can do.

I moved the code over to the Update Event and it can find the control
just fine. Does the control not exist until AFTER the DataGrid is
rendered and we exit the Edit Event Handler? If so, how would I
populate it so that the user can select an item from it before clicking
the Update button?

*** Sent via Developersdex http://www.developersdex.com ***


Nov 19 '05 #4
No. I'm pulling the items in ddlTechs from an SQL database, but ddlTechs is
not bound to anything. ddlTechs is a drop down list that appears on the page
outside of the Datagrid. When I want to edit a row inside the DataGrid, I
want the DropDownList in the DataGrid to populate its items from the
ddlTechs control. But I'm not getting that far.

Response.write(Ctype(e.items.FindControl("ddl3").G etType.ToString)) <--
Typed from memory, may not be correct.

This works fine in the Update event and shows "DropDownList". But when I try
the same thing in the Edit Event, I get the "Object Reference Not Set..."
error.

"Scott Allen" <sc***@nospam.odetocode.com> wrote in message
news:a1********************************@4ax.com...
Hi Terry:

Since you are pulling items from another drop down list it would
depend on the order of events and when the items appear in the other
list. Are you using databinding to populate ddlTechs?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 02 May 2005 08:53:26 -0700, Terry Olsen <to******@hotmail.com>
wrote:
Thanks for the tip. I'll check it out and see what I can do.

I moved the code over to the Update Event and it can find the control
just fine. Does the control not exist until AFTER the DataGrid is
rendered and we exit the Edit Event Handler? If so, how would I
populate it so that the user can select an item from it before clicking
the Update button?

*** Sent via Developersdex http://www.developersdex.com ***

Nov 19 '05 #5
Hi Terry:

Apologies for not getting thorugh to me, I can see now the edit event
must fire before the drop down exists, then you reload the grid, but
of course e.Item must still be pointing to the old item withought a
drop down list.

A solution would be to override ItemDataBound on the DataGrid, then
when the edit item is being bound (e.Item.ItemType ==
ListItemType.EditItem) you can FindControl on the list and populate
it.

Make sense?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 2 May 2005 12:01:49 -0600, "Terry Olsen"
<to******@hotmail.com> wrote:
No. I'm pulling the items in ddlTechs from an SQL database, but ddlTechs is
not bound to anything. ddlTechs is a drop down list that appears on the page
outside of the Datagrid. When I want to edit a row inside the DataGrid, I
want the DropDownList in the DataGrid to populate its items from the
ddlTechs control. But I'm not getting that far.

Response.write(Ctype(e.items.FindControl("ddl3"). GetType.ToString)) <--
Typed from memory, may not be correct.

This works fine in the Update event and shows "DropDownList". But when I try
the same thing in the Edit Event, I get the "Object Reference Not Set..."
error.

"Scott Allen" <sc***@nospam.odetocode.com> wrote in message
news:a1********************************@4ax.com.. .
Hi Terry:

Since you are pulling items from another drop down list it would
depend on the order of events and when the items appear in the other
list. Are you using databinding to populate ddlTechs?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 02 May 2005 08:53:26 -0700, Terry Olsen <to******@hotmail.com>
wrote:
Thanks for the tip. I'll check it out and see what I can do.

I moved the code over to the Update Event and it can find the control
just fine. Does the control not exist until AFTER the DataGrid is
rendered and we exit the Edit Event Handler? If so, how would I
populate it so that the user can select an item from it before clicking
the Update button?

*** Sent via Developersdex http://www.developersdex.com ***


Nov 19 '05 #6
Well, just reading it here doesn't make sense. Let me go play with it and
get back to you.

"Scott Allen" <sc***@nospam.odetocode.com> wrote in message
news:o4********************************@4ax.com...
Hi Terry:

Apologies for not getting thorugh to me, I can see now the edit event
must fire before the drop down exists, then you reload the grid, but
of course e.Item must still be pointing to the old item withought a
drop down list.

A solution would be to override ItemDataBound on the DataGrid, then
when the edit item is being bound (e.Item.ItemType ==
ListItemType.EditItem) you can FindControl on the list and populate
it.

Make sense?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 2 May 2005 12:01:49 -0600, "Terry Olsen"
<to******@hotmail.com> wrote:
No. I'm pulling the items in ddlTechs from an SQL database, but ddlTechs
is
not bound to anything. ddlTechs is a drop down list that appears on the
page
outside of the Datagrid. When I want to edit a row inside the DataGrid, I
want the DropDownList in the DataGrid to populate its items from the
ddlTechs control. But I'm not getting that far.

Response.write(Ctype(e.items.FindControl("ddl3") .GetType.ToString)) <--
Typed from memory, may not be correct.

This works fine in the Update event and shows "DropDownList". But when I
try
the same thing in the Edit Event, I get the "Object Reference Not Set..."
error.

"Scott Allen" <sc***@nospam.odetocode.com> wrote in message
news:a1********************************@4ax.com. ..
Hi Terry:

Since you are pulling items from another drop down list it would
depend on the order of events and when the items appear in the other
list. Are you using databinding to populate ddlTechs?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 02 May 2005 08:53:26 -0700, Terry Olsen <to******@hotmail.com>
wrote:

Thanks for the tip. I'll check it out and see what I can do.

I moved the code over to the Update Event and it can find the control
just fine. Does the control not exist until AFTER the DataGrid is
rendered and we exit the Edit Event Handler? If so, how would I
populate it so that the user can select an item from it before clicking
the Update button?

*** Sent via Developersdex http://www.developersdex.com ***

Nov 19 '05 #7
Hi Terry,

It's little bit tricky problem when using FindControl in
EditCommand event. You know that the editable DatagridItem
has two states, normal and edit. In normal state, it shows
stuff in ItemTemplate tags, on the other hand, in edit
state, it shows stuff in EditItemTemplate tags.
Accordingly using FindControl method you can only lookup
corresponding controls. In EditCommand event, after

DataGrid.EditItemIndex = e.Item.ItemIndex
Datagrid.DataSource = data_source_object
Datagrid.DataBind()

The datagrid.Items(e.Item.ItemIndex) becomes edit item,
but e.Item still keeps in normal state. Hence if you use
datagrid.Items(e.Item.ItemIndex).FindControl("ddl3 "), it
will work.

BTW, Scott's suggestion to use DataGridItem.FindControl is
much better than use TableCell.FindControl, unless you
have different controls with same ID in different cells.
HTH

Elton Wang
el********@hotmail.com
-----Original Message-----

I've got a datagrid set up to display data. I've also got anEdit,Update,Cancel column set up to allow editing of data.

I've got a DropDownList (ID="ddl3")in the EditItemTemplate for a certaincolumn that I need to populate while in Edit Mode.

Two questions: Is there a way to directly use another DropDownList as aDataSource? If so how?

I'm using the following code to try to populate the DropDownList, butgetting an "Object Reference not set to an Instance of an Object" errorwhen trying to add items to it. The column is the 4th column (index=3).
If I remove the code after the "LoadGrid()" line, then everythingpopulates fine except for the DropDownList, which is empty.
Hopefully someone can help me get this code working.

-----------------------------------------
Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e AsSystem.Web.UI.WebControls.DataGridCommandEventArg s) HandlesDataGrid1.EditCommand
DataGrid1.EditItemIndex = e.Item.ItemIndex
btnAddServer.Visible = False
LoadGrid()
Dim ddl As DropDownList =
CType(e.Item.Cells(3).FindControl("ddl3"), DropDownList)
For i As Integer = 0 To ddlTechs.Items.Count - 1
ddl.Items.Add(ddlTechs.Items.Item(i).Value)
Next
End Sub
----------------------------------------

*** Sent via Developersdex http://www.developersdex.com ***.

Nov 19 '05 #8
You guys are awesome! I used both suggestions and got the routine
working. Here is the code that works:

Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles
DataGrid1.EditCommand
DataGrid1.EditItemIndex = e.Item.ItemIndex
btnAddServer.Visible = False
LoadGrid()
Dim ddl As DropDownList =
CType(DataGrid1.Items(e.Item.ItemIndex).FindContro l("ddl3"),
DropDownList)
For i As Integer = 0 To ddlTechs.Items.Count - 1
ddl.Items.Add(ddlTechs.Items.Item(i).Value)
Next
End Sub

Thanks again!

*** Sent via Developersdex http://www.developersdex.com ***
Nov 19 '05 #9
Good deal, Terry, and good advice, Elton.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 02 May 2005 18:48:19 -0700, Terry Olsen <to******@hotmail.com>
wrote:
You guys are awesome! I used both suggestions and got the routine
working. Here is the code that works:

Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArg s) Handles
DataGrid1.EditCommand
DataGrid1.EditItemIndex = e.Item.ItemIndex
btnAddServer.Visible = False
LoadGrid()
Dim ddl As DropDownList =
CType(DataGrid1.Items(e.Item.ItemIndex).FindContr ol("ddl3"),
DropDownList)
For i As Integer = 0 To ddlTechs.Items.Count - 1
ddl.Items.Add(ddlTechs.Items.Item(i).Value)
Next
End Sub

Thanks again!

*** Sent via Developersdex http://www.developersdex.com ***


Nov 19 '05 #10
Okay, now i've got my code completely working. I wish I better
understood the scope of the various ways of doing it (cells vs. items,
etc). Anyhow, here's the completed code for the Edit event. Notice the
last 2 lines concerning the label. I'm taking the value from the label
in the Normal Template and using it to change the SelectedValue in the
DropDownList. Is this the "preferred" way, or is there a better way (as
was pointed out earlier in this thread) of finding the Label Control?

Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles
DataGrid1.EditCommand
DataGrid1.EditItemIndex = e.Item.ItemIndex
btnAddServer.Visible = False
LoadGrid()
Dim ddl As DropDownList =
CType(DataGrid1.Items(e.Item.ItemIndex).FindContro l("ddl3"),
DropDownList)
For i As Integer = 0 To ddlTechs.Items.Count - 1
ddl.Items.Add(ddlTechs.Items.Item(i).Value)
Next
Dim lb As Label = CType(e.Item.Cells(3).FindControl("Label3"),
Label)
ddl.SelectedValue = lb.Text
End Sub

*** Sent via Developersdex http://www.developersdex.com ***
Nov 19 '05 #11

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

Similar topics

8
by: Gilles T. | last post by:
How I can get element ID in the edit mode of datagrid control? If I not in the edit mode, there are no problem. <asp:TemplateColumn ItemStyle-CssClass="grid_column_width_3"...
0
by: mike | last post by:
Hi there: I've read an excellent "how to"-article by Microsoft (no. 306227) - partly cited cited at the end of this email). I have implemented the code related to the part "How to Add a...
1
by: tshad | last post by:
I am using sample code from C# and am trying to convert it to VB.Net in my aspx page. This works in C#: DataGrid oGrid = (DataGrid)oItem.FindControl("DataGrid1"); I have tried all kinds of...
0
by: BG | last post by:
i am thinking that creating datagrid at design time will save me a lot of time when creating programs. however, i ran into a brick wall. i created a template column at design time. i learned...
1
by: Jim Bayers | last post by:
I get this error: System.NullReferenceException: Object reference not set to an instance of an object. And this is the subroutine that throws it: Protected Sub DataUpdate(ByVal Sender As...
0
by: Luis Alonso | last post by:
Hi, I had created a web custom control to ask for user' confirmation on deleting and others process. I put the control in a vb file. The control is really simple: only an image button and a hidden...
5
by: manmit.walia | last post by:
Hello All, I am stuck on a conversion problem. I am trying to convert my application which is written in VB.NET to C# because the project I am working on currently is being written in C#. I tried...
0
by: ashwin | last post by:
I have a Data Grid to display employee names and codes. On clicking a button (outside the datagrid) I perform some calculations which sets the fields of other columns in the datagrid. The problem...
0
by: ashwin | last post by:
I have a Data Grid to display employee names and codes. On clicking a button (outside the datagrid) I perform some calculations which sets the fields of other columns in the datagrid. The problem...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.