是否将控件动态添加到模板化用户控件? [英] Dynamically adding controls to a Templated User Control?

查看:82
本文介绍了是否将控件动态添加到模板化用户控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几周前,我创建了一个模板化的用户控件,大部分基于此处的示例:
http://msdn.microsoft.com/en-us/library/36574bf6 (v = VS.90).aspx

所不同的是,我没有实现"MessageContainer"类,因为我只想要一个空模板,可以在设计时添加想要的任何控件.

这个TUC的运行非常好,但是我遇到了一个我创建东西时没想到的场景.需要将此TUC动态添加到页面,这意味着我也需要在TUC模板中动态添加控件.

我在这里找到了另一个有关如何动态创建模板并将其添加到模板控件的示例:
http://msdn.microsoft.com/en-us/library/y0h809ak (v = vs.71).aspx

第二个示例文章仅讨论"DataList,Repeater和DataGrid控件",但是我认为由于我在这里使用ITemplate接口,因此应该是同一回事.

但是,我无法使它正常工作,我一直在获取对象引用未设置为对象的实例".我尝试填充TUC时发生错误.

这就是我在做什么....

像上面的示例一样,我创建了一个ITemplate类:

A few weeks back I created a Templated User Control, for the most part based on the example here:
http://msdn.microsoft.com/en-us/library/36574bf6(v=VS.90).aspx

The difference is that I did not implement the "MessageContainer" class as I wanted just an empty template that I could add whatever controls I wanted to at design time.

This TUC has been working great, but I ran into a scenario I hadn''t anticipated when I created the thing. The need to dynamically add this TUC to a page, which means that I would need to dynamically add the controls within the template of the TUC as well.

I found another example here on how to dynamically create a Template and add it to the Templated Control:
http://msdn.microsoft.com/en-us/library/y0h809ak(v=vs.71).aspx

This second example article discusses only the "DataList, Repeater, and DataGrid controls" but I figure since I am using the ITemplate interface here, it should be the same thing.

However, I am unable to get this to work, I keep getting an "Object reference not set to an instance of an object." error when I attempt to populate the TUC.

Here''s what I am doing....

Like the example above I created an ITemplate class:

public class XPCTemplate : ITemplate
{
    private readonly Control _control;
    public XPCTemplate(Control control)
    {
        _control = control;
    }

    public void InstantiateIn(Control container)
    {
        container.Controls.Add(_control);
    }
}



然后,在后面的测试页代码中,我尝试加载并动态显示TUC:



Then, in the test page code-behind I attempt to load up and display the TUC dynamically:

ExpandCollapseRegion xpcRegion;  // The Templated User Control

protected void Page_Load(object sender, EventArgs e)
{
    PlaceHolder ph;
    // ...dynamically create some labels, textboxes, etc...;

    // Create an instance of the TUC
    xpcRegion = new ExpandCollapseRegion();

    // Pass into the TUC's template the controls dynamically created above
    xpcRegion.LayoutTemplate = new XPCTemplate(ph);

    // add the dynamic TUC to the page
    phDynamicUC.Controls.Add(xpcRegion);

    phDynamicUC.Controls.Add(new LiteralControl("<br />"));
}



测试页HTML来源:



Test page HTML Source:

<body>
    <form id="form1"  runat="server">
    <div>
    
    Dynamically Loading User Control
    <br />
    <asp:PlaceHolder ID="phDynamicUC" runat="server" />
    
    </div>
    </form>
</body>




运行页面时,在"container.Controls.Add(_control);"上收到未将对象引用设置为对象实例"错误. XPCTemplate类的代码行.当我调试测试页面和TUC控件时,TUC的代码在TUC的Page_Init()方法期间将XPCTemplate接收到其LayoutTemplate中,但是当XPCTemplate中返回的InstantiateIn()事件随后立即触发时,容器" 参数为NULL.

我不确定为什么会这样,就像XPCTemplate类的InstantiateIn()方法试图在TUC内实际设置PlaceHolder控件,而不只是传递内容一样.也许这应该是这样,而我在TUC方面却缺少一些东西来允许这种行为?

这是我创建的第一个TUC,并且同样是第一次尝试动态填充/加载它,因此,我确定我缺少完成此操作所需的内容.任何帮助将不胜感激.

-Andrew




When I run the page, I get the "Object reference not set to an instance of an object" error on the "container.Controls.Add(_control);" line of the XPCTemplate class. When I debug the test page and TUC control, the code of the TUC receives the XPCTemplate into its LayoutTemplate during the TUC''s Page_Init() method, but when the InstantiateIn() event back in the XPCTemplate fires immediately afterwards, the "container" argument is NULL.

I''m not sure why this is happening, it''s like the InstantiateIn() method of the XPCTemplate class is trying to actually set the PlaceHolder control within the TUC rather than just passing the contents. Maybe this is supposed to be the way and I am missing something on the TUC side to allow this behavior?

This is the first TUC I have created and likewise the first time trying to dynamically fill/load it, so I am sure I am missing something needed to accomplish this. Any help is greatly appreciated.

-- Andrew

推荐答案

找到了解决问题的方法,这就是我加载TUC的方式.

错误:
Found the solution to the problem, which was how I was loading the TUC.

Incorrect:
xpcRegion = new ExpandCollapseRegion();




正确:




Correct:

xpcRegion = (ExpandCollapseRegion)LoadControl("ExpandCollapseRegion.ascx");



进行此简单更改即可解决该问题.另外,发现我可以使用CompiledTemplateBuilder()方法放弃对自定义ITemplate类的需要.真正简化了代码.

-安德鲁



Making this simple change took care of the problem. Also, found that I could forego the need for a custom ITemplate class by using the CompiledTemplateBuilder() method. Really simplifies the code.

-- Andrew


这篇关于是否将控件动态添加到模板化用户控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆