为什么我的ASP.NET FormView总是呈现EmptyTemplate?

时间:2012-03-19 09:07:20

标签: c# asp.net data-binding objectdatasource formview

我有一个FormView绑定到放置在usercontrol中的ObjectDataSource,使用以下代码进行初始化:

<asp:ObjectDataSource ID="odsCampaign" runat="server"
    DataObjectTypeName="code.model.Campaign"
    TypeName="code.model.Campaign"
    SelectMethod="LoadCampaign">
    <SelectParameters>
        <asp:QueryStringParameter Name="code" QueryStringField="id" Type="Int32" />
    </SelectParameters>
</asp:ObjectDataSource>

<%-- ... --%>

<asp:FormView ID="fvCampaign" runat="server"
    DataSourceID="odsCampaign">
    <EmptyDataTemplate>
        <span>No campaign loaded.</span>
    </EmptyDataTemplate>
    <ItemTemplate>
        <span>Campaign</span>
        <span><%# Eval("Subject") %></span>

        <%-- ... --%>
    </ItemTemplate>
</asp:FormView>

我背后的代码看起来像这样:

// CampaignCallCollection inherits from IEnumerable<CampaignCall>
public class Campaign : CampaignCallCollection
{
    // Some property to show
    public string Subject { get; set; }

    // Constructor
    public Campaign(int code)
        : base()
    {
        // Initialize the object based on the primary key passed to the constructor
        InitializeCampaign(code);
    }

    private void InitializeCampaign(int code)
    {
        // Initialization Code
    }

    // Loading method for ObjectDataSource
    public static Campaign LoadCampaign(int code)
    {
        // Return new instance of an initialized campaign object.
        Campaign oCampaign = new Campaign(code);
        //throw new Exception(oCampaign.Subject);
        return oCampaign;
    }
}

但是,如果我启用了异常,则会将正确的主题文本作为异常消息传递。但我的FormView总是渲染EmptyTemplate。我真的没有在这里看到错误。任何人都可以帮我解决这个问题吗?

提前致谢!

1 个答案:

答案 0 :(得分:0)

是不是因为期待广告系列列表而不是单个广告系列?