Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Dynamically created control in Datagrid problem

Reply
Thread Tools

Dynamically created control in Datagrid problem

 
 
Elton Wang
Guest
Posts: n/a
 
      02-25-2005
Hi Wanky,

You can use

datagrid.Items(rowIndex).Cells(colIndex).Contrls(0[or 1))

to get the reference.

Or you can assign ID to a control when you add it, so late
you can get it.

BTW, it's better to add control in ItemCreated event.

HTH

Elton Wang



>-----Original Message-----
>I created a datagrid with 4 bound columns and 1 template

column.
>I am using the ITEMDATABOUND event to add a control to

the template column,
>if column (3) has a value of 'checkox' then add the

checkbox
>control else, add a textbox control to the template

column.
>
>That part works fine, the user can enter data.
>
>My problem is getting the data from that dynamically

created cell.
>I'm using a button_click event in hopes of cycling

through the datagrid
>and retrieving the template column values to write back

to the dataset.
>
>The only reference I know of is the e argument, but I

cannot expose it
>in my custom routine.
>
>How can I reference the template column, it does not have

an ID and the
>FINDCONTROL so far has been ineffective for me.
>
>--
>Message posted via http://www.dotnetmonster.com
>.
>

 
Reply With Quote
 
 
 
 
Elton Wang
Guest
Posts: n/a
 
      02-25-2005
Which language do you use, c# or vb.net?


>-----Original Message-----
>thanks,
>I'll try that.
>I sent an email with an attachment of the print screen.
>
>I can't seen to grad the right event handler in the item
>created event.
>
>--
>Message posted via http://www.dotnetmonster.com
>.
>

 
Reply With Quote
 
 
 
 
Elton Wang
Guest
Posts: n/a
 
      02-25-2005
Followng code snippet shows how to loop thru datagrid
items (rows) to get control reference:

Dim ctrl As Control
For Each item As DataGridItem In datagrid.Items
ctrl = item.Cells(4).Controls(0) ' or try Controls(1)
depending on how to you add it
' If you assign ctrl ID, you can
ctrl = item.FindControl("ctrlID")

Dim ctrlType As String = item.Cells(2).Text
If ctrlType.Equals("Textbox") Then
Dim txtBox As TextBox = CType(ctrl, TextBox)
' Process as text box
Else
Dim ck As CheckBox = CType(ctrl, CheckBox)
' process as checkbox
End If

' Or you can dynamically find control type
If TypeOf ctrl Is TextBox Then
Dim txtBox As TextBox = CType(ctrl, TextBox)
' Process as text box
Else
Dim ck As CheckBox = CType(ctrl, CheckBox)
'process as checkbox
End If
Next

HTH

Elton Wang


>-----Original Message-----
>vb.net
>
>--
>Message posted via http://www.dotnetmonster.com
>.
>

 
Reply With Quote
 
Elton Wang
Guest
Posts: n/a
 
      02-25-2005
I prefer to use same ID for both TextBox and CheckBox, so
it is easy to get control first then to figure out what
kind control it is. And it's better to use For Each loop
rather than For index loop.

HTH

Elton

>-----Original Message-----
>I haven't tried your stuff yet, but this is how I created

the controls.
>For some reason the ID reference doesn't take.
>
>Private Sub DataGrid1_ItemDataBound(ByVal sender As

Object, ByVal e As
>System.Web.UI.WebControls.DataGridItemEventArgs ) Handles
>DataGrid1.ItemDataBound
>
>If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType

=
>ListItemType.AlternatingItem Then
> Dim str = RTrim(DataGrid1.DataSource.Tables
>(DataGrid1.DataMember).Rows(e.Item.DataSetIndex)( 2))
> Select Case RTrim(DataGrid1.DataSource.Tables
>(DataGrid1.DataMember).Rows(e.Item.DataSetIndex)( 2))
> Case "Textbox", "Other", "memo"
> Dim tb = New

System.Web.UI.WebControls.TextBox
> e.Item.Cells(4).Controls.Add(tb)
> e.Item.Cells(4).ID = "Txtbx"
> Case "Checkbox"
> Dim cb = New

System.Web.UI.WebControls.CheckBox
> e.Item.Cells(4).Controls.Add(cb)
> e.Item.Cells(4).ID = "Chkbx"
> End Select
> End If
> End Sub
>
>--
>Message posted via http://www.dotnetmonster.com
>.
>

 
Reply With Quote
 
Elton Wang
Guest
Posts: n/a
 
      02-28-2005
I'm not sure what causes the problem. It might be that
the .NET can't save dynamically created control data after
data binding in viewstate (I just guess.). If that, you
can statically add both TextBox and Checkbox (given
different id, i.e. txtID, ckID) in cell(4), then when in
ItemDataBound, set one invisible accordingly. In your
GetValues method,
using
If ctlType.Equals("Textbox") Then
Dim tb As TextBox = CType(dg.FindContrl("txtID"),
TextBox)
Else
Dim cb As CheckBox = CType(dg.FindContrl("ckID"),
CheckBox)
End If

HTH

Elton Wang

>-----Original Message-----
>Hello again,
>
>I tried the suggested code, and I got the following error:
>Specified argument was out of the range of valid values.

Parameter name:
>index
>
>..controls(0) or .controls(1) both return the error.
>
>It seems that the control I created during the

ITEMDATABOUND event
>doesn't really exist as far the datagrid is concerned.
>I mean, I can see it, enter data into, but I am finding

it extremely
>difficult to extract that data out of the control
>
>When I try the ID route, the code doesn't error but the

variable I used
>during the assignment returns a value of nothing.
>
>CODE AREA

************************************************** ******
> Private Sub GetValues()
> Dim c As Control
> Dim dg As DataGridItem
> For Each dg In DataGrid1.Items
> c = dg.Cells(4).Controls(0)
> Dim ctlType As String = RTrim(dg.Cells

(2).Text)
> If ctlType.Equals("Textbox") Then
> Dim tb As TextBox = CType(c, TextBox)
> Else
> Dim cb As CheckBox = CType(c, CheckBox)
> End If
> Next
> End Sub
>
>--
>Message posted via http://www.dotnetmonster.com
>.
>

 
Reply With Quote
 
Elton Wang
Guest
Posts: n/a
 
      03-01-2005
I don't think there are big problems to get solution. In
one way or another we can find a means to get the
reference of the control when the page is postback.


>-----Original Message-----
>I have a hunch that viewstate maybe an area to

concentrate on.
>I'm starting to think that I'm losing state as I envoke

the
>getvalues sub. At some point the control is recreated

before
>I can trap the value.
>
>My problem is that this database will have over 200 data

entry
>points, which could change depending on the user. I can

think of no other
>way to efficiently create a form to handle the data

points entry, other
>than a datagrid.
>
>Any ideas?
>
>--
>Message posted via http://www.dotnetmonster.com
>.
>

 
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
Affecting a dynamically created drop down from another dynamically created drop down. msimmons ASP .Net 0 07-16-2009 03:17 PM
Managing ViewState of a dynamically created Custom Composite Server Control -(where the original is also dynamically created) dickster ASP .Net Building Controls 0 12-08-2005 09:32 AM
Dynamically created control in Datagrid problem Elton Wang ASP .Net Datagrid Control 0 03-02-2005 02:26 PM
Problem with Event handling for the dynamically created control Pravin ASP .Net 2 01-14-2005 04:33 AM
Problem Accessing WebForm Controls from a Dynamically created control (C#) R Duke ASP .Net 2 04-25-2004 08:21 AM



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