Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将用户控件列表绑定到gridview_C#_Asp.net_Gridview - Fatal编程技术网

C# 将用户控件列表绑定到gridview

C# 将用户控件列表绑定到gridview,c#,asp.net,gridview,C#,Asp.net,Gridview,我拼命寻找,但徒劳无功。我想将用户控件列表(.ascx)绑定到gridview。我在代码隐藏中初始化控件: List<myControl> ctrls = new List<myControl>(); myControl ctr = LoadControl("~/Control.ascx") as myControl; ctr.Name = ... // ... ctrls.Add(myControl); // add new control to the collect

我拼命寻找,但徒劳无功。我想将用户控件列表(.ascx)绑定到gridview。我在代码隐藏中初始化控件:

List<myControl> ctrls = new List<myControl>();
myControl ctr = LoadControl("~/Control.ascx") as myControl;
ctr.Name = ...
// ...
ctrls.Add(myControl); // add new control to the collection

在页面中,如果(!IsPostBack),则加载条件为
的事件。这不起作用:将显示对象的表示。然而,当我把控件放在面板中时,所有控件都工作了。

不要使用GridView进行此操作。使用一个。并将其绑定到数据,而不是控件列表。例如:

<asp:Repeater runat="server" id="ControlsRepeater">
    <ItemTemplate>
       <uc:MyControl runat="server" />
    </ItemTemplate>
</asp:Repeater>

如果您想要分页,那么您应该利用(如果您使用它,那么它将为您处理)和Linq函数。

为什么要在GridView中使用它们?那有什么用呢?为什么不把它们放在一个面板中呢?对于n个元素的分页:/I我想你这里有一个。构建一个寻呼机控件,或者只是在页面中构建一个寻呼机。您不需要GridView的所有开销。即使你想使用GridView,你也走错了路。您不会将其绑定到控件列表。您可以将其绑定到数据,并让模板系统为您创建自定义控件。若你们不需要多列,你们应该使用一个中继器。用面板分页的轨道?
<asp:Repeater runat="server" id="ControlsRepeater">
    <ItemTemplate>
       <uc:MyControl runat="server" />
    </ItemTemplate>
</asp:Repeater>
protected void Page_Load(object sender, EventArgs e)
    {
    if(!IsPostBack)
        {
        var myData=GetData(); //should return some type of ICollection representing your data to bind to
        ControlsRepeater.DataSource=myData;
        ControlsRepeater.DataBind();
        }
    }