By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,900 Members | 1,446 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,900 IT Pros & Developers. It's quick & easy.

Referencing controls in a DataGrid

P: n/a
From VS.NET 03 AND VB.NET

I added a dropdown list to a data grid control in response to the
EditCommand event to force users to pick values from a list and not
type them in. I used the following code:

'Hide the existing txt box
Dim txt As TextBox
txt = CType(e.Item.Cells(0).Controls(0), TextBox)
txt.Visible = False

'display a ddl instead
Dim ds As New DataSet
ds = dbCalls.GetData
Dim ddl As New DropDownList
ddl.ID = "myID"
ddl.DataSource = ds
ddl.DataTextField = "myTxt"
ddl.DataValueField = "myID"
ddl.DataBind()
ddl.Attributes.Add("class", "myCSS")
e.Item.Cells(0).Controls.Add(ddl)

This works great (everything displays as intended in the UI).
However, when I code the UpdateCommand I cannot find the DropDownList
control I created above. When I debug and add a watch for
e.Item.Cells(0) it only shows a single control
(e.Item.Cells(0).controls(0) which is the text box). How can I
reference the DropDownList control? I am using the following code:

Dim ddlMaintCatID As DropDownList
ddlMaintCatID = CType(e.Item.Cells(0).Controls(X), DropDownList)

For X i tried the following vales (0 and 1). For 0 I get and invalid
cast error becuase controls(0) is a text box and for 1 I get an index
out of range error.

What am I missing???

thanks in advance for your time and assistance.
Scott
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ddlMaintCatID = CType(e.Item.Cells(0).FindControl("myID"), DropDownList)

the above will work if you used

ddl.ID = "myID" and "myID" is a string, if it is the unique DB id then you
will have to substitue something to determine the currect ID (use a hidden
column and grab the text property or something)
"mystical_potato" <He********@FingDie.Now> wrote in message
news:27********************************@4ax.com...
From VS.NET 03 AND VB.NET

I added a dropdown list to a data grid control in response to the
EditCommand event to force users to pick values from a list and not
type them in. I used the following code:

'Hide the existing txt box
Dim txt As TextBox
txt = CType(e.Item.Cells(0).Controls(0), TextBox)
txt.Visible = False

'display a ddl instead
Dim ds As New DataSet
ds = dbCalls.GetData
Dim ddl As New DropDownList
ddl.ID = "myID"
ddl.DataSource = ds
ddl.DataTextField = "myTxt"
ddl.DataValueField = "myID"
ddl.DataBind()
ddl.Attributes.Add("class", "myCSS")
e.Item.Cells(0).Controls.Add(ddl)

This works great (everything displays as intended in the UI).
However, when I code the UpdateCommand I cannot find the DropDownList
control I created above. When I debug and add a watch for
e.Item.Cells(0) it only shows a single control
(e.Item.Cells(0).controls(0) which is the text box). How can I
reference the DropDownList control? I am using the following code:

Dim ddlMaintCatID As DropDownList
ddlMaintCatID = CType(e.Item.Cells(0).Controls(X), DropDownList)

For X i tried the following vales (0 and 1). For 0 I get and invalid
cast error becuase controls(0) is a text box and for 1 I get an index
out of range error.

What am I missing???

thanks in advance for your time and assistance.
Scott

Nov 18 '05 #2

P: n/a
Hi Potato:

I have some articles with C# source that might help. They talk about
the basics of using FindControl and using DropDownLists in a grid.

In Search Of ASP.NET controls
http://odetocode.com/116.aspx

Embedding DropDownList Controls In A Grid
http://odetocode.com/Articles/231.aspx
HTH,
--
Scott
http://www.OdeToCode.com

On Tue, 07 Sep 2004 14:37:16 GMT, mystical_potato
<He********@FingDie.Now> wrote:
From VS.NET 03 AND VB.NET

I added a dropdown list to a data grid control in response to the
EditCommand event to force users to pick values from a list and not
type them in. I used the following code:

'Hide the existing txt box
Dim txt As TextBox
txt = CType(e.Item.Cells(0).Controls(0), TextBox)
txt.Visible = False

'display a ddl instead
Dim ds As New DataSet
ds = dbCalls.GetData
Dim ddl As New DropDownList
ddl.ID = "myID"
ddl.DataSource = ds
ddl.DataTextField = "myTxt"
ddl.DataValueField = "myID"
ddl.DataBind()
ddl.Attributes.Add("class", "myCSS")
e.Item.Cells(0).Controls.Add(ddl)

This works great (everything displays as intended in the UI).
However, when I code the UpdateCommand I cannot find the DropDownList
control I created above. When I debug and add a watch for
e.Item.Cells(0) it only shows a single control
(e.Item.Cells(0).controls(0) which is the text box). How can I
reference the DropDownList control? I am using the following code:

Dim ddlMaintCatID As DropDownList
ddlMaintCatID = CType(e.Item.Cells(0).Controls(X), DropDownList)

For X i tried the following vales (0 and 1). For 0 I get and invalid
cast error becuase controls(0) is a text box and for 1 I get an index
out of range error.

What am I missing???

thanks in advance for your time and assistance.
Scott


Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.