views:

571

answers:

4

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?

A: 

I have the same issue. Did you find a solution?

pe3
No I couldn't make auto complete work.
Christopher Edwards
A: 

Did you find a solution?

mbakr
No, but if you read my edit it's only auto complete that is affected.We should file a bug report on MSDN...
Christopher Edwards
+1  A: 

I have found solution for this problem

Step 1 - Add the following propert to your UserControl

[ MergableProperty(false), DefaultValue((string)null), 
Editor("System.Web.UI.Design.WebControls.DataControlFieldTypeEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
    typeof(UITypeEditor)), 
PersistenceMode(PersistenceMode.InnerProperty)]
public virtual DataControlFieldCollection Columns
{
    get
    {
        return GridView1.Columns;
    }
}

Secand - you can set you column collection inside your usercontrol tag like

 <uc1:TControl ID="TControl1" runat="server" >
    <Columns>
      <asp:BoundField DataField="ID" HeaderText="UserID" />
      <asp:BoundField DataField="Name" HeaderText="UserName" />
    </Columns>
    </uc1:TControl>

Visual Studio will enable autocomplete for columns Property but designer will throw Exception

Finaly you can bind gridview inside your user control

mbakr
A: 

Does it working on design view

mzain