简体   繁体   English

ASP.Net用户控制模板实例化

[英]ASP.Net User Control Template Instantiation

I created a user control that has a template container. 我创建了一个具有模板容器的用户控件。

<cc:SearchResultItem ID="SearchResultItem1" Customer='<%# ((Customer)(((RepeaterItem)Container).DataItem)) %>' runat="server">                                                
    <NameTemplate>
        <%# Container.Name %>
    </NameTemplate>
</cc:SearchResultItem> 

This is control is placed in a repeater which lists some customers. 该控件放置在列出一些客户的转发器中。 The customer is than bound to the user control. 然后,将客户绑定到用户控件。 When the name template is instantiated in the container, the customer object is not yet available, but I need to access its name because it needs to get parsed before. 在容器中实例化名称模板后,客户对象尚不可用,但是我需要访问其名称,因为它需要在之前进行解析。

protected void Page_Init(object sender, EventArgs e)
{
    if (nameTemplate != null )
    {
        // customer is null here, it is avaiable only after Page_Init...
        NameContainer container = new NameContainer(customer.Id, Parse(customer.Name));
        nameTemplate.InstantiateIn(container);
        placeHolder.Controls.Add(container);
    }
}

Question: How can I access properties set for the user control BEFORE the template container is instantiated? 问题:在实例化模板容器之前,如何访问为用户控件设置的属性? Thanks in advance! 提前致谢!

I don't think you can. 我认为你不能。 How could you access a databound property of the control before the control is databound? 在控件进行数据绑定之前,如何访问控件的数据绑定属性?

From the code that you've posted, I don't actually see a need to do this during Init, but I'm probably missing something. 从您发布的代码中,我实际上没有发现在Init期间需要执行此操作,但是我可能缺少了一些东西。 Can you clarify why you need the customer during Init and not during Load or PreRender? 您能否阐明为什么在初始化期间而不是在Load或PreRender期间需要客户?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM