Home All Groups Group Topic Archive Search About

Dynamically created control in Datagrid problem

Author
25 Feb 2005 4:45 PM
L Wanky via DotNetMonster.com
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

Author
25 Feb 2005 6:59 PM
Elton Wang
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
elton_w***@hotmail.com


>-----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
Show quoteHide quote
>FINDCONTROL so far has been ineffective for me.
>
>--
>Message posted via http://www.dotnetmonster.com
>.
>
Author
25 Feb 2005 7:42 PM
L Wanky via DotNetMonster.com
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
Author
25 Feb 2005 8:13 PM
Elton Wang
Which language do you use, c# or vb.net?


Show quoteHide quote
>-----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
>.
>
Author
25 Feb 2005 8:21 PM
L Wanky via DotNetMonster.com
vb.net

--
Message posted via http://www.dotnetmonster.com
Author
25 Feb 2005 8:48 PM
L Wanky via DotNetMonster.com
Elton,

I get the following error when I tried this code:

For y = 0 To maxrec
If DataGrid1.Items(y).Cells(4).Controls(0) Is GetType  (TextBox) Then
   Dim str As String = CType(DataGrid1.Items(y).Cells(4).Controls(0),
TextBox).Text()
End If
y += 1
Next

Specified argument was out of the range of valid values. Parameter name:
index

--
Message posted via http://www.dotnetmonster.com
Author
25 Feb 2005 9:10 PM
Elton Wang
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
elton_w***@hotmail.com

Show quoteHide quote
>-----Original Message-----
>vb.net
>
>--
>Message posted via http://www.dotnetmonster.com
>.
>
Author
25 Feb 2005 9:36 PM
L Wanky via DotNetMonster.com
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
Author
25 Feb 2005 10:00 PM
Elton Wang
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
Show quoteHide quote
>                    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
>.
>
Author
28 Feb 2005 7:45 PM
L Wanky via DotNetMonster.com
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
Author
28 Feb 2005 9:58 PM
Elton Wang
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

Show quoteHide quote
>-----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
Show quoteHide quote
>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
>.
>
Author
1 Mar 2005 3:08 PM
L Wanky via DotNetMonster.com
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
Author
1 Mar 2005 6:44 PM
Elton Wang
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
Show quoteHide quote
>than a datagrid.
>
>Any ideas?
>
>--
>Message posted via http://www.dotnetmonster.com
>.
>
Author
2 Mar 2005 2:26 PM
Elton Wang
Hi Wanky,

Following is solution. I'm sure it works.

' Add both textbox and checkbox in datagrid
<asp:TemplateColumn HeaderText="Choice">
    <ItemTemplate>
        <asp:CheckBox ID="ckChoice"
Runat="server"></asp:CheckBox>
        <asp:TextBox ID="txtChoice"
Runat="server"></asp:TextBox>
    </ItemTemplate>
</asp:TemplateColumn>


' in ItemDataBound set one of control invisible according
to type

Dim drv As DataRowView = e.Item.DataItem

If drv(FieldTypeIndex).ToString.Equals("Textbox") Then 
    e.Item.FindControl("ckChoice").Visible = False
Else
    e.Item.FindControl("txtChoice").Visible = False
End If


' in save button Click retrieve control type depending on
type filed

For Each item As DataGridItem In Me.dgdSearch.Items
    If item.Cells(FieldTypeIndex).Text.Equals("Textbox")
Then
       Dim txtCtrl As TextBox = CType(item.FindControl
("txtChoice"), TextBox)
       ' process
    Else
       Dim ck As CheckBox = CType(item.FindControl
("ckChoice"), CheckBox)      
       ' process
    End If
Next

If you still have problem, please let me know.

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
Show quoteHide quote
>FINDCONTROL so far has been ineffective for me.
>
>--
>Message posted via http://www.dotnetmonster.com
>.
>
Author
8 Mar 2005 7:54 PM
L Wanky via DotNetMonster.com
thanks,
I will try this!!

--
Message posted via http://www.dotnetmonster.com