C# 将用户控件列表绑定到gridview
我拼命寻找,但徒劳无功。我想将用户控件列表(.ascx)绑定到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
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();
}
}