ASP.NET,重复ID和模板化控件...还有更优雅的方法吗? [英] ASP.NET, Duplicate IDs, and Templated Control... Is there a more elegant way?
问题描述
我的应用程序界面提供了最终用户输入以创建标记,而我正在使用它们的模板控件:
My application interface provides end-user input to create markup, that of which I'm using a templated control for:
<me:MyControl>
<TemplateA>
I'm inside of an ITemplate
</TemplateA>
<TemplateB>
So what, I am too.
</TemplateB>
</me:MyControl>
我需要在这些模板的网络控件中允许重复的控件ID。现在我知道我不能在一个模板中有重复的ID,但是我可以这样做:
I need to allow duplicate control ID's within web controls inside of these templates. Now I know I can't have a duplicate ID inside of a single template, but I can do this:
<me:MyControl>
<TemplateA>
<me:Textbox Id="ABC" />
</TemplateA>
<TemplateB>
<me:Textbox Id="ABC" />
</TemplateB>
</me:MyControl>
我要做的是通过属性而不是通过模板制作:
What I want to do is instead of making the templates via property:
<TemplateContainer(GetType(HelloWorld))>
<PersistenceMode(PersistenceMode.InnerProperty)>
Public Property TemplateA() As ITemplate
Protected Overrides Sub CreateChildControls()
If TemplateA IsNot Nothing Then TemplateA.InstantiateIn(Me)
If TemplateB IsNot Nothing Then TemplateB.InstantiateIn(Me)
MyBase.CreateChildControls()
End Sub
我想通过使用实现ITemplate的自定义类类型来制作它们。但是,当我这样做时,会收到有关重复ID的错误:
I want to make them by using a custom class type that Implements ITemplate. However, when I do this, I get errors about duplicate IDs:
这里是一个示例: https://msdn.microsoft.com/zh-cn/library/0e39s2ck.aspx
这样,我就可以拥有属性,甚至可以覆盖控件构建器类,以引入自定义控件而不会造成丑陋的标记。
That way I can have properties and even override the control builder class to pull in custom controls without ugly markup.
因此,代替这个:
<me:MyControl>
<TemplateA>
<me:BaseControl Hello="World" Foo="Bar">
More controls inside
</me:BaseControl>
</TemplateA>
</me:MyControl>
我可以这样做:
<me:MyControl>
<TemplateA Hello="World" Foo="Bar">
More controls inside
</TemplateA>
</me:MyControl>
因此,最终,最重要的是解决重复的控件ID问题,我需要最终用户具有设置此ID的能力,因此无需考虑这些ID。我希望能够做到这一点:
So ultimately, more than anything this is all about solving the duplicate control ID issue, and I need the end user to have the ability to set this ID, so leaving out the IDs is out of the question. I want to be able to do this:
<me:MyControl>
<TemplateA PropertyA="Hello" PropertyB="World">
<asp:Textbox Id="Test" />
</TemplateA>
<TemplateA PropertyA="Foo" PropertyB="Bar">
<asp:Textbox Id="Test" />
</TemplateA>
</me:MyControl>
因此,我需要能够在自己的控件实例中拥有重复的ID,除了将它们放在模板内部之外,没有其他方法可以执行此操作,但是我不希望templateA,templateB等。我需要能够将属性传递给这些模板(将它们构建为类而不是内部属性) ,但是当我这样做时仍然会得到重复的ID错误。)
So, I need to be able to have duplicate IDs within their own instance of the control, which I know no other way to do this other than them being inside of a template, but I don't want templateA, templateB, etc. I need to be able to pass properties to these templates (building them as a class instead of an inner property, but still getting duplicate ID errors when I do it this way).
我在vb.net中编写了示例,但欢迎使用C#支持。一般而言,这是一个asp.net问题。
I wrote my examples in vb.net, but C# support is welcome. This is an asp.net question in general.
推荐答案
尝试xml linq
Try xml linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xmlHeader = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
"<Root xmlns:me =\"abc\" xmlns:asp =\"def\"></Root>";
XDocument doc = XDocument.Parse(xmlHeader);
XElement root = (XElement)doc.FirstNode;
XNamespace meNs = root.GetNamespaceOfPrefix("me");
XNamespace aspNs = root.GetNamespaceOfPrefix("asp");
XElement control = new XElement(meNs + "MyControl");
root.Add(control);
var inputs = new[] {
new { PropertyA="Hello", PropertyB="World", Id="Test"},
new { PropertyA="Foo", PropertyB="Bar", Id="Test"},
};
foreach (var input in inputs)
{
control.Add(new XElement("TemplateA", new object[] {
new XAttribute("PropertyA", input.PropertyA),
new XAttribute("PropertyB", input.PropertyB),
new XElement(aspNs + "Textbox", new XAttribute("Id", input.Id))
}));
}
}
}
}
这篇关于ASP.NET,重复ID和模板化控件...还有更优雅的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!