How do I expose the columns collection of GridView control that is inside a user control

Posted by Christopher Edwards on Stack Overflow See other posts from Stack Overflow or by Christopher Edwards
Published on 2009-05-27T13:12:19Z Indexed on 2010/04/01 7:03 UTC
Read the original article Hit count: 438

Filed under:
|

See edit.

I want to be able to do this in the aspx that consumes the user control.

<uc:MyControl ID="MyGrid" runat="server">
     <asp:BoundField DataField="FirstColumn" HeaderText="FirstColumn" />
     <asp:BoundField DataField="SecondColumn" HeaderText="SecondColumn" />
</uc>

I have this code (which doesn't work). Any ideas what I am doing wrong?

VB

Partial Public Class MyControl
    Inherits UserControl

    <System.Web.UI.IDReferenceProperty(GetType(DataControlFieldCollection))> _
    Public Property Columns() As DataControlFieldCollection
        Get
            Return MyGridView.Columns
        End Get
        Set(ByVal value As DataControlFieldCollection)
            ' The Columns collection of the GridView is ReadOnly, so I rebuild it
            MyGridView.Columns.Clear()
            For Each c As DataControlField In value
                MyGridView.Columns.Add(c)
            Next
        End Set
    End Property

    ...

End Class

C#

public partial class MyControl : UserControl
{
    
    [System.Web.UI.IDReferenceProperty(typeof(DataControlFieldCollection))]
    public DataControlFieldCollection Columns {
        get { return MyGridView.Columns; }
        set {
            MyGridView.Columns.Clear();
            foreach (DataControlField c in value) {
                MyGridView.Columns.Add(c);
            }
        }
    }

    ...

}

EDIT:

Actually it does work, but auto complete does not work between the uc:MyControl opening and closing tags and I get compiler warnings:-

  • Content is not allowed between the opening and closing tags for element 'MyControl'.

  • Validation (XHTML 1.0 Transitional): Element 'columns' is not supported.

  • Element 'BoundField' is not a known element. This can occur if there is a compilation error in the Web site, or the web.config file is missing.

So I guess I need to use some sort of directive to tell the complier to expect content between the tags.

Any ideas?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about usercontrols