Listview grouptemplate - 代码背后

时间:2012-11-11 18:36:18

标签: c# asp.net listview code-behind

我期待以编程方式加载组模板和布局模板,但我收到以下错误消息:

必须在ListView' Listview1'上指定组占位符。何时定义GroupTemplate。通过将其ID属性设置为" ListView_GC $ groupPlaceHolder1"来指定组占位符。组占位符控件还必须指定runat =" server"。

    protected void Listview1_Init(object sender, EventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("in init");
        Listview1.ItemPlaceholderID = "ListView_UC$itemPlaceHolder1";
        Listview1.GroupPlaceholderID = "ListView_GC$groupPlaceHolder1";
        Listview1.GroupItemCount = 3;
        Listview1.LayoutTemplate = LoadTemplate("~/Controls/ThumbnailView_LayoutTemplate.ascx");
        Listview1.GroupTemplate = LoadTemplate("~/Controls/ThumbnailView_GroupTemplate.ascx");
        Listview1.ItemTemplate = LoadTemplate("~/Controls/ThumbnailView_ItemTemplate.ascx");

    }


    void Listview1_LayoutCreated(object sender, EventArgs e)
    {
       Listview1.Controls.RemoveAt(0);


        Control newLayoutContainer = new Control();

        Listview1.LayoutTemplate.InstantiateIn(newLayoutContainer);
        Listview1.GroupTemplate.InstantiateIn(newLayoutContainer); 
        var userControl = newLayoutContainer.Controls[0];
        var usergControl = newLayoutContainer.Controls[1];

        userControl.ID = "ListView_UC";
        usergControl.ID = "ListView_GC";

        Listview1.Controls.Add(newLayoutContainer);

    }

有人可以帮忙吗?

编辑:

控件的标记......

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ThumbnailView_GroupTemplate.ascx.cs" Inherits="myproject.Controls.ThumbnailView_GroupTemplate" %>
<tr>
    <asp:PlaceHolder ID="itemPlaceHolder1" runat="server" />
</tr>

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ThumbnailView_LayoutTemplate.ascx.cs" Inherits="myproject.Controls.ThumbnailView_LayoutTemplate" %>
<table>
    <asp:PlaceHolder ID="groupPlaceHolder1" runat="server" />
</table>

0 个答案:

没有答案