Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Referencing controls in a DataGrid

Reply
Thread Tools

Referencing controls in a DataGrid

 
 
mystical_potato
Guest
Posts: n/a
 
      09-07-2004
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
 
Reply With Quote
 
 
 
 
Thomas Dodds
Guest
Posts: n/a
 
      09-07-2004
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" <> wrote in message
news:...
> 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



 
Reply With Quote
 
 
 
 
Scott Allen
Guest
Posts: n/a
 
      09-07-2004
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
<> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble referencing controls within list controls 2obvious ASP .Net 2 01-11-2005 08:42 PM
referencing controls in controls DC Gringo ASP .Net Building Controls 2 09-16-2004 10:40 PM
referencing controls in controls DC Gringo ASP .Net Web Controls 2 09-16-2004 10:40 PM
Trouble referencing controls within list controls 2obvious ASP .Net 0 09-13-2004 10:38 PM
Referencing controls on user controls Jim Corey ASP .Net 1 01-26-2004 07:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57