容器数据值不是通过使用模板化用户控件来实现的

发布于 2024-12-25 06:10:00 字数 1987 浏览 1 评论 0原文

我创建了一个模板化的用户控件。我不想通过发布我用来创建控件的所有代码来使这篇文章变得臃肿,但足以说明我相当确定代码是正确的。我将发布一些片段来表明我确实知道执行此操作的正确方法。首先是我的 ITemplate 实现:

private ITemplate _NutritionLabelTemplate = null;
[TemplateContainer(typeof(NutritionLabelContainer))]
[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate NutritionLabelTemplate
{
    get { return _NutritionLabelTemplate; }
    set { _NutritionLabelTemplate = value; }
}

然后是我的 INamingContainer 接口的实现:

public class NutritionLabelContainer : Control, INamingContainer
{
}

我的模板标记有一个名为“phNutritionLabel_Template”的占位符控件,并穿过其他等等等等,我得到了类似的东西:

phNutritionLabel_Template.Controls.Clear();
if (NutritionLabelTemplate != null)
{
    NutritionLabelContainer nContainer = new NutritionLabelContainer();
    nContainer.calcium = calcium;
    nContainer.calories = calories;

    NutritionLabelTemplate.InstantiateIn(nContainer);
    phNutritionLabel_Template.Controls.Add(nContainer);
}

之后,我将此模板化的 UserControl 添加到我的网页并为了测试它,我添加了以下代码:

    <uc1:NutritionalLabel_Template ID="NutritionalLabel_Template1" runat="server" 
    servingSize="28"
    calories="46">
     <NutritionLabelTemplate>
        <h1>Template Calories</h1>
        <span style="font-size:large; font-style:italic;"></span>
         <asp:Label ID="Label1" runat="server">
         <%#Container.calories %>
         </asp:Label>
        <br />
     </NutritionLabelTemplate>
     </uc1:NutritionalLabel_Template>

当我运行页面时,#Container.calories 的值不可见,它完全是空白的。我已经调试了我的代码并逐步执行它,我可以看到这些值显然是在模板化控件的 NamingContainer 中设置的,并且这些值正在通过 NamingContainer 传递到占位符。

我还可以通过将以下代码放置在网页的代码隐藏文件中来验证值的设置,然后查看页面上反映的值。但除此之外,什么也没有。

Label1.Text = NutritionalLabel_Template1.calories;

我以前见过这个问题出现,但不记得原因或解决方案是什么。任何人都可以提供任何指示吗?

我只创建过几次这些类型的控件,所以它对我来说有点新鲜。任何帮助将不胜感激。我被难住了。

谢谢

I have created a Templated UserControl. I don't want to bloat this post by posting all the code I used to create the control but suffice to say that I am fairly certain that the code is correct. I will post a few snippets to show that I do know the proper way to do this. First is my ITemplate implementation:

private ITemplate _NutritionLabelTemplate = null;
[TemplateContainer(typeof(NutritionLabelContainer))]
[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate NutritionLabelTemplate
{
    get { return _NutritionLabelTemplate; }
    set { _NutritionLabelTemplate = value; }
}

Then the implementation of my INamingContainer Interface:

public class NutritionLabelContainer : Control, INamingContainer
{
}

My template markup has a Placeholder control named "phNutritionLabel_Template" and cutting through the other blah blah blah, I've got something like:

phNutritionLabel_Template.Controls.Clear();
if (NutritionLabelTemplate != null)
{
    NutritionLabelContainer nContainer = new NutritionLabelContainer();
    nContainer.calcium = calcium;
    nContainer.calories = calories;

    NutritionLabelTemplate.InstantiateIn(nContainer);
    phNutritionLabel_Template.Controls.Add(nContainer);
}

Afterward, I add this templated UserControl to my webpage and to test it I add the following code:

    <uc1:NutritionalLabel_Template ID="NutritionalLabel_Template1" runat="server" 
    servingSize="28"
    calories="46">
     <NutritionLabelTemplate>
        <h1>Template Calories</h1>
        <span style="font-size:large; font-style:italic;"></span>
         <asp:Label ID="Label1" runat="server">
         <%#Container.calories %>
         </asp:Label>
        <br />
     </NutritionLabelTemplate>
     </uc1:NutritionalLabel_Template>

When I run the page the value of #Container.calories isn't visible, it's totally blank. I've debugged my code and stepping through it I can see that the values are clearly being set in the NamingContainer of the Templated Control and that the values are being carried over to the Placeholder via the NamingContainer.

I can also verify the values are set by placing the following code in the code-behind file of the webpage and then see the values reflected on the page. But otherwise, nothing.

Label1.Text = NutritionalLabel_Template1.calories;

I have seen this problem arise before and can't remember what the cause or resolution was. Can anyone provide any pointers?

I've only created these types of controls a few times so it's a bit new to me. Any help would be appreciated. I'm stumpped.

Thx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

脱离于你 2025-01-01 06:10:00

我从未收到对这篇文章的任何回复,因此无论为什么没有人能够提供有关该问题的任何线索,我弄清楚了问题是什么,并认为我会将解决方案发布给可能遇到类似问题的任何人,以节省您一些时间不必要的挫败感。 ;-)

原来我忘记将 DataBind() 方法添加到默认页面的 Page_Load 事件中。根据 Microsoft 的说法,此方法可确保将数据从源绑定到服务器控件,并且通常在通过数据库查询检索数据集后使用。但由于大多数控件自动执行数据绑定,因此您通常不需要显式调用此方法。

但是,当您创建自定义模板化数据绑定控件时,该方法通常也会被重写。但就我而言,我覆盖了模板化控件中的 PageInit;不是数据绑定方法。因此显然应该对调用页面的 Page_Load 事件中的方法进行显式调用,以确保在这种情况下绑定模板化控件和数据。

I never received any responses to this post so regardless of why no one could provide any clues to the problem, I figured out what the problem was and thought I'd post the solution for anyone who may potentially have the similiar issue to save you some needless frustration. ;-)

Turns out that I'd forgotten to add the DataBind() method to the Page_Load event of the default page. Per Microsoft, this method ensures a binding of data from a source to a server control and it's commonly used after retrieving a dataset through a database query. But since most controls perform data binding automatically, you typically should not need to make an explicit call to this method.

However, the method is also commonly overridden when you create a custom templated data-bound control. But in my case, I overrode the PageInit in the Templated control; not the Databind method. So apparently an explicit call should be made to the method in the calling page's Page_Load event to ensure the templated control and the data are bound in this case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文