System.Web.UI.UserControl没有名为ItemTemplate的属性

时间:2013-12-17 08:11:00

标签: asp.net

在尝试创建模板化Web用户控件时,我遇到的问题是System.Web.UI.UserControl没有名为ItemTemplate的属性。

我该如何解决?

Ascx文件代码是:

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="MyTemplatedControl.ascx.cs" Inherits="MyTemplatedControl" %>
<asp:Placeholder runat="server" ID="ph1" />

.ascx.cs文件代码是:

using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public partial class MyTemplatedControl : System.Web.UI.UserControl
{
    private ITemplate itemTemplate = null;
    private ITemplate alternatingItemTemplate = null;
    [TemplateContainer(typeof(ItemContainer))]
    public ITemplate ItemTemplate
    { 
        get{return itemTemplate;}
        set { itemTemplate = value; }
    }
    public ITemplate AlternatingItemTemplate 
    {
        get { return alternatingItemTemplate; }
        set { alternatingItemTemplate = value; }
    }
    private bool isOdd(int i)
    {
        if (i % 2 == 0)
            return false;
        else
            return true;
    }
    void Page_init()
    {
        String[] student = { "sam", "ram", "geet", "rosy", "linda", "vandana" };
        for (int i = 0; i < 6; i++)
        {
            ItemContainer container = new ItemContainer(student[i]);
            if (isOdd(i))
            {
                alternatingItemTemplate.InstantiateIn(container);
            }
            else
            {
                itemTemplate.InstantiateIn(container);
            }
            this.ph1.Controls.Add(container);
        }

        }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}
public class ItemContainer : Control, System.Web.UI.INamingContainer
{
 private String _s_name;
    internal ItemContainer(String name)
    {
        _s_name = name;
    }
    public String name 
    {
        get { return _s_name; }
    }
}

.aspx文件代码是:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register src="~/MyTemplatedControl.ascx" TagName="MyTemplatedControl" TagPrefix="uc1" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Test page for Templated User Control</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <uc1:MyTemplatedControl runat="server" ID="MyTemplatedControl">
    <ItemTemplate>
    <font color="blue">
    <asp:Label runat="server" ID="Label2" Text='<%#Container.name%>' />
    </font>
    <hr />
    </ItemTemplate>
    <AlternatingItemTemplate>
    <font color="green">
    <asp:Label runat="server" ID="Label1" Text='<%#Container.name%>' />
    </font>
    <hr />
    </AlternatingItemTemplate> 

    </uc1:MyTemplatedControl>

    </div>
    </form>
</body>
</html>

.aspx.cs文件代码是这样的:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataBind();

    }
}

0 个答案:

没有答案