如何公开用户控件内的 GridView 控件的列集合 [英] How do I expose the columns collection of GridView control that is inside a user control

查看:27
本文介绍了如何公开用户控件内的 GridView 控件的列集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看编辑.

我希望能够在使用用户控件的 aspx 中执行此操作.

<asp:BoundField DataField="FirstColumn" HeaderText="FirstColumn"/><asp:BoundField DataField="SecondColumn" HeaderText="SecondColumn"/></uc>

我有这个代码(它不起作用).知道我做错了什么吗?

VB

部分公共类 MyControl继承用户控件<System.Web.UI.IDReferenceProperty(GetType(DataControlFieldCollection))>_公共属性 Columns() 作为 DataControlFieldCollection得到返回 MyGridView.Columns结束获取Set(ByVal value As DataControlFieldCollection)' GridView 的 Columns 集合是只读的,所以我重建它MyGridView.Columns.Clear()For Each c As DataControlField In valueMyGridView.Columns.Add(c)下一个结束集最终财产...结束类

C#

公共部分类 MyControl : UserControl{[System.Web.UI.IDReferenceProperty(typeof(DataControlFieldCollection))]公共数据控制字段集合列 {得到 { 返回 MyGridView.Columns;}放 {MyGridView.Columns.Clear();foreach (DataControlField c 的值) {MyGridView.Columns.Add(c);}}}...}

实际上它确实有效,但自动完成在 uc:MyControl 开始和结束标记之间不起作用,并且我收到编译器警告:-

  • 元素MyControl"的开始和结束标记之间不允许有内容.

  • 验证(XHTML 1.0 Transitional):不支持元素columns".

  • 元素BoundField"不是已知元素.如果网站中存在编译错误或缺少 web.config 文件,就会发生这种情况.

所以我想我需要使用某种指令来告诉编译器期望标签之间的内容.

有什么想法吗?

解决方案

我已经找到了这个问题的解决方案

步骤 1 - 将以下属性添加到您的 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)]公共虚拟 DataControlFieldCollection 列{得到{返回 GridView1.Columns;}}

Secand - 你可以在你的 usercontrol 标签中设置你的列集合,比如

 <asp:BoundField DataField="ID" HeaderText="UserID"/><asp:BoundField DataField="Name" HeaderText="UserName"/></列></uc1:TControl>

<块引用>

Visual Studio 将为列属性启用自动完成功能,但设计器会抛出异常

最后,您可以在用户控件中绑定 gridview

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?

解决方案

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

这篇关于如何公开用户控件内的 GridView 控件的列集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆