在DataGrid中需要FindControl的帮助 [英] Need help with FindControl in DataGrid

查看:68
本文介绍了在DataGrid中需要FindControl的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经设置了数据网格来显示数据。我还有一个

编辑,更新,取消列设置以允许编辑数据。


我有一个DropDownList(ID EditItemTemplate中的=" ddl3")我需要在编辑模式下填充某个

列。


两个问题:有没有办法直接使用另一个DropDownList作为

DataSource?如果是这样的话?


我正在使用以下代码来尝试填充DropDownList,但是

得到一个对象引用未设置为对象的实例错误

尝试添加项目时。该列是第4列(索引= 3)。


如果我删除LoadGrid()之后的代码。行,然后一切

填充正常,除了DropDownList,它是空的。


希望有人能帮助我使这段代码正常工作。


-----------------------------------------

Private Sub DataGrid1_EditCommand(ByVal source As Object,ByVal e As

System.Web.UI.WebControls.DataGridCommandEventArgs)句柄

DataGrid1.EditCommand

DataGrid1.EditItemIndex = e.Item.ItemIndex

btnAddServer.Visible = False

LoadGrid()

Dim ddl As DropDownList =

CType(e.Item.Cells(3).FindControl(" ddl3"),DropDownList)

对于i as Integer = 0到ddlTechs.Items。数 - 1

ddl.Items.Add(ddlTechs.Items.Item(i).Value)

下一页

结束子
----------------------------------------


***通过开发人员指南 http://www.developersdex.com ***

解决方案



嗨特里:


我可以提供的一个提示是使用FindControl项目而不是

Cell。这允许列移动,您不需要更改

代码,即e.Item.FindControl(" ...")。我有一篇文章

更多信息: http:/ /odetocode.com/Articles/116.aspx


另外,你可能想要通过调试器一步一步找出

究竟是什么变量未定义(无)。


-

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

On Sun,5月1日2005 18:56:27 -0700,Terry Olsen< to ****** @ hotmail.com>

写道:


我已经设置了数据网格来显示数据。我还有一个
编辑,更新,取消列设置以允许编辑数据。

我有一个DropDownList(ID =" ddl3")在编辑模式下我需要填充的某个
列的EditItemTemplate。

两个问题:有没有办法直接使用另一个DropDownList作为数据源?如果是这样的话?

我正在使用以下代码来尝试填充DropDownList,但是获取对象引用未设置为对象的实例。错误
尝试向其添加项目时。该列是第4列(索引= 3)。

如果我删除LoadGrid()之后的代码。然后,除了DropDownList之外,所有东西都会很好地填充,这是空的。

希望有人可以帮助我使这段代码正常工作。

----- ------------------------------------
Private Sub DataGrid1_EditCommand(ByVal source As Object,ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArg s)句柄
DataGrid1.EditCommand
DataGrid1.EditItemIndex = e.Item.ItemIndex
btnAddServer.Visible = False
LoadGrid()
Dim ddl As DropDownList =
CType(e.Item.Cells(3).FindControl(" ddl3"),DropDownList)
对于i as Integer = 0到ddlTechs .Items.Count - 1
ddl.Items.Add(ddlTechs.Items.Item(i).Value)
下一页
结束子
-------- --------------------------------

***通过Developersdex发送 http://www.developersdex.com ***




感谢您的提示。我会检查一下,看看我能做些什么。


我将代码移到了更新事件中,它可以找到控件

精细。在DataGrid渲染后我们退出编辑事件处理程序之前,控件是否不存在?如果是这样,我将如何填充它以便用户可以在点击

更新按钮之前从中选择一个项目?


***通过开发人员指南 http://www.developersdex.com 发送***


嗨特里:


既然你从另一个下拉列表中提取物品,它将会是

取决于事件的顺序以及项目何时出现在另一个

列表中。您是否正在使用数据绑定来填充ddlTechs?


-

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


周一,02 2005年5月08:53:26 -0700,Terry Olsen< to ****** @ hotmail.com>

写道:

谢谢你小费。我会检查一下,看看我能做些什么。

我把代码移到了更新事件上,它可以找到控件
就好了。在DataGrid呈现之后,控件是否不存在,我们退出编辑事件处理程序?如果是这样,我将如何填充它以便用户可以在点击更新按钮之前从中选择一个项目?

***通过Developersdex发送 http://www.developersdex.com ***





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 ***

解决方案


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 ***




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 ***


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 ***




这篇关于在DataGrid中需要FindControl的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆