创建一个简单的模板化控件。有问题
我正在尝试创建一个非常简单的模板化控件。我以前从未这样做过,但我知道如果我包含模板功能,我过去创建的许多控件都会受益匪浅 - 所以我现在正在学习。
我遇到的问题是我的模板输出在页面上,但我的属性值没有输出。所以我得到的只是包含在模板中的静态文本。
我必须正确执行某些操作,因为该控件不会导致任何错误,因此它知道我的公共财产存在。 (例如,如果我尝试使用 Container.ThisDoesntExist 它会抛出异常)。
我希望得到一些帮助。我可能只是一个完整的布偶,错过了一些东西。关于简单模板化服务器控件的在线教程似乎很少,所以如果您知道的话我想了解一下。
我的代码的精简版本如下。
非常感谢, James
这是我的控件代码:
[ParseChildren(true)]
public class TemplatedControl : Control, INamingContainer
{
private TemplatedControlContainer theContainer;
[TemplateContainer(typeof(TemplatedControlContainer)), PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate ItemTemplate { get; set; }
protected override void CreateChildControls()
{
Controls.Clear();
theContainer = new TemplatedControlContainer("Hello World");
this.ItemTemplate.InstantiateIn(theContainer);
Controls.Add(theContainer);
}
}
这是我的容器代码:
[ToolboxItem(false)]
public class TemplatedControlContainer : Control, INamingContainer
{
private string myString;
public string MyString
{
get
{
return myString;
}
}
internal TemplatedControlContainer(string mystr)
{
this.myString = mystr;
}
}
这是我的标记:
<my:TemplatedControl runat="server">
<ItemTemplate>
<div style="background-color: Black; color: White;">
Text Here: <%# Container.MyString %>
</div>
</ItemTemplate>
</my:TemplatedControl>
I'm trying to create a really simple templated control. I've never done it before, but I know a lot of my controls I have created in the past would have greatly benefited if I included templating ability - so I'm learning now.
The problem I have is that my template is outputted on the page but my property value is not. So all I get is the static text which I include in my template.
I must be doing something correctly because the control doesn't cause any errors, so it knows my public property exists. (e.g. if I try to use Container.ThisDoesntExist it throws an exception).
I'd appreciate some help on this. I may be just being a complete muppet and missing something. Online tutorials on simple templated server controls seem few and far between, so if you know of one I'd like to know about it.
A cut down version of my code is below.
Many Thanks,
James
Here is my code for the control:
[ParseChildren(true)]
public class TemplatedControl : Control, INamingContainer
{
private TemplatedControlContainer theContainer;
[TemplateContainer(typeof(TemplatedControlContainer)), PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate ItemTemplate { get; set; }
protected override void CreateChildControls()
{
Controls.Clear();
theContainer = new TemplatedControlContainer("Hello World");
this.ItemTemplate.InstantiateIn(theContainer);
Controls.Add(theContainer);
}
}
Here is my code for the container:
[ToolboxItem(false)]
public class TemplatedControlContainer : Control, INamingContainer
{
private string myString;
public string MyString
{
get
{
return myString;
}
}
internal TemplatedControlContainer(string mystr)
{
this.myString = mystr;
}
}
Here is my mark up:
<my:TemplatedControl runat="server">
<ItemTemplate>
<div style="background-color: Black; color: White;">
Text Here: <%# Container.MyString %>
</div>
</ItemTemplate>
</my:TemplatedControl>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该在控件上调用 DataBind 方法。
一种可能性是在 CreateChildControls() 方法中添加 DataBind 调用:
protected override void CreateChildControls()
{
Controls.Clear();
you should invoke method DataBind on your control.
one possibility is to add DataBind call in CreateChildControls()method:
protected override void CreateChildControls()
{
Controls.Clear();