2

I have created a custom control to implement a table we use all over the place. It has been working fine for a while and now I was asked to add a new column type that would allow any valid asp.net markup to be put in it and that markup will be rendred within that table column.

I created the following class:

    [PersistChildren(false), ParseChildren(true)]
    public class UserDefinedMarkupColumn : GridColumnBase
    {
        private Collection<WebControl> _innerMarkup = new Collection<WebControl>();

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public virtual Collection<WebControl> InnerMarkup { get { return _innerMarkup; } }

        public override System.Web.UI.WebControls.TableCell CreateCell(object dataSourceObject, GridColumnBase col, out CustomGridRow extraRow, Action<object, System.Web.UI.WebControls.CommandEventArgs> handler, bool isMobileLayout = false)
        {
            TableCell newCell = new TableCell();

            foreach (var ctrl in _innerMarkup)
            {
                newCell.Controls.Add(ctrl);
            }

            extraRow = null;
            return newCell;
        }
    }

and this markup for the column:

<CustomGrid:UserDefinedMarkupColumn ID="markup" HeaderText="Some Markup">
   <InnerMarkup>
      <asp:TextBox runat="server" />
   </InnerMarkup>
</CustomGrid:UserDefinedMarkupColumn>

The problem is that when I hit CreateCell the _innerMarkup collection is always empty. I saw several samples using a ControlsCollection but the issue is that GridColumnBase does not inherit from Control, it creates a TableCell which does.

EDIT: I also tried adding a PlaceHolder control and using it's ControlsCollection, but it is empty as well.

private PlaceHolder _innerControl = new PlaceHolder();

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
public virtual ControlCollection InnerMarkup { get { return _innerControl.Controls; } }
Elad Lachmi
  • 10,140
  • 11
  • 69
  • 127
  • Have you tried using Templates: http://stackoverflow.com/questions/2610625/asp-net-itemplate-ways-of-declaring – Philip Pittle Oct 19 '14 at 08:25
  • @PhilipPittle - I tried using a template. The issue is that the `Column` class itself does not inherit from `Control` or `WebControl`. I think this is why nothing is actually rendred and the template remains null. – Elad Lachmi Oct 19 '14 at 09:06
  • Before we dive into the code, run a Wireshark trace and tell us what he query strings look like on the postback. This will lead us to the next step... – JWP Oct 24 '14 at 21:04
  • @user1522548 - What postback? This does not even work on the initial `GET`. – Elad Lachmi Oct 25 '14 at 05:27
  • Ok put a breakpoint at the foreach statement and tell us what's in _innerMarkup. – JWP Oct 25 '14 at 15:44
  • That's what I thought initially. This line does nothing: private Collection _innerMarkup = new Collection(); Except new up a collection of type WebControl. Webcontrol is a class type not an instance of something unless it's static. So somewhere in the PGM you have to add a webcontrol to the colleciton. – JWP Oct 27 '14 at 18:57
  • @user1522548 - I am adding controls to it via the markup. The collection is the default property of the control and is populated in the markup. But the instances of the controls are never created by the framework. – Elad Lachmi Oct 28 '14 at 13:56
  • But you just told us the value is NULL... I don't understand. – JWP Oct 28 '14 at 15:19
  • @user1522548 - I am adding controls via the markup, but the instances of the controls are not created and the collection is empty. – Elad Lachmi Oct 29 '14 at 07:20

0 Answers0