FormView 内带有属性 [TemplateInstance(TemplateInstance.Single)] 的模板化控件出错
我创建了一个具有单个 ContentTemplate 的自定义控件,其中我使用 [TemplateInstance(TemplateInstance.Single)] 属性装饰 ITemplate 属性,以使内部的所有控件直接可用,而无需使用 FindControl 访问它们。
但是,如果我根据以下内容在 Formviews InsertItemTemplate 和 EditItemTemplate 中使用我的控件...
<asp:FormView runat="server" ID="formView">
<InsertItemTemplate>
<my:Control runat="server" ID="myControl">
<ContentTemplate>
<asp:TextBox runat="server" ID="textBox" />
</ContentTemplate>
</my:Control>
</InsertItemTemplate>
<EditItemTemplate>
<my:Control runat="server" ID="myControl">
<ContentTemplate>
<asp:TextBox runat="server" ID="textBox" />
</ContentTemplate>
</my:Control>
</EditItemTemplate>
</asp:FormView>
...我收到一条错误消息,因为我的 ContentTemplate 内的两个文本框(或任何其他控件)具有相同的 ID:
“类 XXX 已包含文本框的定义”
我觉得这很奇怪,因为 FormView 的 InsertItemTemplate 和 EditItemTemplate 没有用提到的属性装饰,因此内部的所有控件都应将其范围限定到其模板。对于上面示例中的 ID“myControl”也是如此,不会产生冲突...
有人知道如何解决这个问题吗?现在,我可以在模板中使用不同的 ID:s,但我希望它按原样工作,因为这些 FormView 模板内的控件具有相同的 ID:s 是很常见的。
I've created a Custom Control that has a single ContentTemplate where I've decorated the ITemplate property with the [TemplateInstance(TemplateInstance.Single)] attribute to make all controls inside directly available without having to use FindControl to access them.
However, if I use my control in a Formviews InsertItemTemplate and EditItemTemplate according to the following...
<asp:FormView runat="server" ID="formView">
<InsertItemTemplate>
<my:Control runat="server" ID="myControl">
<ContentTemplate>
<asp:TextBox runat="server" ID="textBox" />
</ContentTemplate>
</my:Control>
</InsertItemTemplate>
<EditItemTemplate>
<my:Control runat="server" ID="myControl">
<ContentTemplate>
<asp:TextBox runat="server" ID="textBox" />
</ContentTemplate>
</my:Control>
</EditItemTemplate>
</asp:FormView>
...I get an error message because the two textboxes (or any other control) inside my ContentTemplate has the same ID:
"The class XXX already contains a definition for textBox"
I find this very strange since the InsertItemTemplate and EditItemTemplate of the FormView are NOT decorated with the mentioned attribute so all controls inside should get scoped to their template. This is also true for the ID "myControl" in the sample above, that gives no clash...
Does anyone have any idea how to solve this? For now I can use different ID:s in the templates but I'd like it to work as is since it's so common that controls inside these FormView templates have the same ID:s.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道这已经晚了,但是当您定义
TemplateInstance.Single
时,框架允许直接引用控件,因此 ID 必须是唯一的。因此,您不需要FindControl()
,但还需要一个唯一的 ID 来引用。对于像 ListView 这样具有 ItemTemplate 的控件,请使用TemplateInstance.Multiple
。这就是某些模板因行为而异的原因。I know this is late, but when you define
TemplateInstance.Single
, the framework allows direct reference to the control, and therefore ID's have to be unique. Therefore, you don't needFindControl()
, but you also need a unique ID to refer by. Controls like ListView, where it has an ItemTemplate, useTemplateInstance.Multiple
. That's the reason why some template vary by behavior.