Can't get a reference to user control in datagrid

Ask a Question related to ASP.NET Data Grid Control, Design and Development.

  1. #1

    Default Can't get a reference to user control in datagrid

    I created a Web User Control in my project and need to use it in a
    datagrid. The data in this control needs to be updated and the control
    has several properties that need to be databound.

    Using the designer I created a a dataGrid with an Edit, Update, Cancel
    column, and an empty template column.

    I have overridden ItemDataBound to instantiate my control, set the
    properties using DataItems, and add this control to the template column
    in the grid. This part works and i see the data. When the users
    clciks edit they get an editable version of my control which also work
    as expected.

    When the users clicks update I need to read the data from my control
    and save it to the database. The problem is I cannot find the control.
    It should be in e.Item.Cells(1) but there are no controls in the cell.
    The only other Cells is 0 and it is not here.

    How do I reference my User Control?

    Don

    Don Guest

  2. Similar Questions and Discussions

    1. "Object reference not set" when loading a textbox in a user control
      Hi All, I have a user control that shows name and address for a person in a text box. When the page hosting the control loads up, it calls a...
    2. Dynamically loading user control into Placeholder gives Object reference not set to an instance of an object
      I've created user controls that contain listboxes that are dynamically populated from the database. In the html view of the user control...
    3. Reference Datagrid Footer Control
      In a datagrid footer, how could I programmatically set a footer textbox text? E.g.. the datagrid (dg1) will bind to a datareader based on a query....
    4. Datagrid in a User control
      Hello, I have the following structure on a signle page: DG1 |--Nested NDG1 (in a usercontrol) |--Nested NDG2 (in another usercontrol) Now...
    5. object reference-error with programmatically loading user control
      Hi there! I have a problem with programmatically adding user controls to my mobile webforms. If I load my usercontrol programmatically (in the...
  3. #2

    Default Can't get a reference to user control in datagrid

    Hi Don,

    You can try e.FindControl("your control ID") to get the
    reference.

    HTH

    Elton Wang
    [email]elton_wang@hotmail.com[/email]

    >-----Original Message-----
    >I created a Web User Control in my project and need to
    use it in a
    >datagrid. The data in this control needs to be updated
    and the control
    >has several properties that need to be databound.
    >
    >Using the designer I created a a dataGrid with an Edit,
    Update, Cancel
    >column, and an empty template column.
    >
    >I have overridden ItemDataBound to instantiate my
    control, set the
    >properties using DataItems, and add this control to the
    template column
    >in the grid. This part works and i see the data. When
    the users
    >clciks edit they get an editable version of my control
    which also work
    >as expected.
    >
    >When the users clicks update I need to read the data from
    my control
    >and save it to the database. The problem is I cannot
    find the control.
    > It should be in e.Item.Cells(1) but there are no
    controls in the cell.
    > The only other Cells is 0 and it is not here.
    >
    >How do I reference my User Control?
    >
    >Don
    >
    >.
    >
    Elton Wang Guest

  4. #3

    Default Re: Can't get a reference to user control in datagrid

    e does not have a FindControl method but e.item does. I tried it but
    it does not work.

    Don Guest

  5. #4

    Default Re: Can't get a reference to user control in datagrid

    My mistake.

    It's

    e.Item.FindControl("your control ID")

    >-----Original Message-----
    >e does not have a FindControl method but e.item does. I
    tried it but
    >it does not work.
    >
    >.
    >
    Elton Wang Guest

  6. #5

    Default RE: Can't get a reference to user control in datagrid

    Hello Don,

    You should be able to reference the control like:
    UCSimple uc =
    (UCSimple)this.DataGrid1.Items[intRow].Cells[_UCColumn].Controls[_UCControlPosition];

    or from within the grid itself:
    UCSimple uc = (UCSimple)e.Item.Cells[_UCColumn].Controls[_UCControlPosition];
    where _UCOlumn refers to the column, and _UCControlPosition refers to the
    position in the cell.

    I recently wrote a blog about this:
    [url]http://timstall.dotnetdevelopersjournal.com/read/1121938.htm[/url]
    including a simple download code sample that has a UC and host page.
    > It should be in e.Item.Cells(1)
    The structure sounds right. Can you see the control present in the grid? Can
    you reference it absolutely (i.e. without respect to the specific event 'e')?
    In other words, maybe there's a problem in putting the control in the grid,
    not referencing it.



    "Don" wrote:
    > I created a Web User Control in my project and need to use it in a
    > datagrid. The data in this control needs to be updated and the control
    > has several properties that need to be databound.
    >
    > Using the designer I created a a dataGrid with an Edit, Update, Cancel
    > column, and an empty template column.
    >
    > I have overridden ItemDataBound to instantiate my control, set the
    > properties using DataItems, and add this control to the template column
    > in the grid. This part works and i see the data. When the users
    > clciks edit they get an editable version of my control which also work
    > as expected.
    >
    > When the users clicks update I need to read the data from my control
    > and save it to the database. The problem is I cannot find the control.
    > It should be in e.Item.Cells(1) but there are no controls in the cell.
    > The only other Cells is 0 and it is not here.
    >
    > How do I reference my User Control?
    >
    > Don
    >
    >
    Tim Stall Guest

  7. #6

    Default Can't get a reference to user control in datagrid

    On the item command of the grid
    use this e.item.fincontrol("Control Name")

    You have to Ctype the control by the control class and not the ID name.
    i.e. Controls_MyControl. This is the name given to the control in the code page.

    Then it's just a case of

    dim mycontol as Controls_MyControl = ctype( e.item.fincontrol("Control Name"),Controls_MyControl).
    Unregistered Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139