Dynamically created control in Datagrid problem

Discussion in 'ASP .Net Datagrid Control' started by Elton Wang, Feb 25, 2005.

  1. Elton Wang

    Elton Wang Guest

    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
    >.
    >
     
    Elton Wang, Feb 25, 2005
    #1
    1. Advertising

  2. Elton Wang

    Elton Wang Guest

    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
    >.
    >
     
    Elton Wang, Feb 25, 2005
    #2
    1. Advertising

  3. Elton Wang

    Elton Wang Guest

    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
    >.
    >
     
    Elton Wang, Feb 25, 2005
    #3
  4. Elton Wang

    Elton Wang Guest

    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
    >.
    >
     
    Elton Wang, Feb 25, 2005
    #4
  5. Elton Wang

    Elton Wang Guest

    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
    >.
    >
     
    Elton Wang, Feb 28, 2005
    #5
  6. Elton Wang

    Elton Wang Guest

    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
    >.
    >
     
    Elton Wang, Mar 1, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. R Duke
    Replies:
    2
    Views:
    4,652
    =?Utf-8?B?SG1udA==?=
    Apr 25, 2004
  2. Pravin
    Replies:
    2
    Views:
    332
    Pravin
    Jan 14, 2005
  3. Replies:
    5
    Views:
    414
  4. msimmons
    Replies:
    0
    Views:
    514
    msimmons
    Jul 16, 2009
  5. Elton Wang

    Dynamically created control in Datagrid problem

    Elton Wang, Mar 2, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    145
    Elton Wang
    Mar 2, 2005
Loading...

Share This Page