2016-09-04 21 views
0

我的应用程序接口提供最终用户输入创建的标记,这其中我使用了一个模板控制:ASP.NET,重复ID和模板控制...是否有更优雅的方式?

<me:MyControl> 
    <TemplateA> 
    I'm inside of an ITemplate 
    </TemplateA> 
    <TemplateB> 
    So what, I am too. 
    </TemplateB>   
</me:MyControl> 

我需要让这些模板的内部Web控件中重复的控件ID的。现在我知道,我不能有一个模板内重复ID,但我可以这样做:

<me:MyControl> 
    <TemplateA> 
    <me:Textbox Id="ABC" /> 
    </TemplateA> 
    <TemplateB> 
    <me:Textbox Id="ABC" /> 
    </TemplateB>   
</me:MyControl> 

我想要做的是,而不是通过属性使模板:

<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:

下面是一个例子:https://msdn.microsoft.com/en-us/library/0e39s2ck.aspx

这样我可以有属性,甚至可以取代控制生成器类的自定义控件拉不难看的标记。

因此,不是这样的:

<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是不可能的。我希望能够做到这一点:

<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错误这条路 )。

我在vb.net写了我的例子,但c#支持欢迎带着微笑。这是一般的asp.net问题。任何帮助是极大的赞赏。对此感到困难。

回答

0

尝试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)) 
       })); 
      } 
     } 
    } 
} 
+0

我没有这个,所以我道歉回应。我不明白在允许用户操作模板化控件并使用controlbuilder解析ascx文件的内容时这将如何工作。 – user1447679

相关问题