2009-03-23 3 views
13

ASP.NET에서 사용자 정의 컨트롤을 사용하여 사용자 지정 태그를 정의 할 수 있음을 알고 있습니다. 그러나 내가 아는 한 이러한 컨트롤에 속성 만 추가 할 수 있습니다. 더 복잡한 데이터를 임베드 할 수 있기를 바랍니다.사용자 지정 자식 요소가있는 ASP.NET의 사용자 지정 요소

<myControls:MyGraph id="myGraph1" runat="server"> 
    <colors> 
    <color>#abcdef</color> 
    <color>#123456</color> 
    </colors> 
</myControls:MyGraph> 

ASP.NET에서 가능한가요? ListView를 확장해야합니까? 아니면 더 정확한 솔루션이 있습니까?

답변

17

확실히 가능합니다. 귀하의 예를 들어 수업과 같습니다

[ParseChildren(true)] 
class MyGraph : WebControl { 
    List<Color> _colors = new List<Color>(); 
    [PersistenceMode(PersistenceMode.InnerProperty)] 
    public List<Color> Colors { 
     get { return _colors; } 
    } 
} 

class Color { 
    public string Value { get; set; } 
} 

그리고 실제 마크 업은 다음과 같습니다 같은 purpoces에 대한

당신은 할 수 없습니다
<myControls:MyGraph id="myGraph1" runat="server"> 
    <Colors> 
    <myControls:Color Value="#abcdef" /> 
    <myControls:Color Value="#123456" /> 
    </Colors> 
</myControls:MyGraph> 
+0

고마워요 .. 손으로 만든 서버 컨트롤에 관한 모든 전문 용어가 곧바로 답을 얻는 것은 정말 어렵습니다. 뒤늦은 지혜로 내부 요소를 속성으로 취급하고 주목해야 할 것은 많은 의미를가집니다. 건배! – CResults

0

사용자의 UserControl. 위에서 언급 한대로 Control 또는 WebControl을 상속합니다.

+0

왜 충돌하는 답변이 게시되어 있습니까? – cchamberlain

3
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 

namespace ComponentDemo 
{ 
    [ParseChildren(true)] 
    public class MyGraph : System.Web.UI.WebControls.WebControl 
    { 
     private List<Color> _colors; 

     public MyGraph() : base() { ;} 

     [PersistenceMode(PersistenceMode.InnerProperty)] 
     public List<Color> Colors 
     { 
      get 
      { 
       if (null == this._colors) { this._colors = new List<Color>(); } 
       return _colors; 
      } 
     } 
    } 

    public class Color 
    { 
     public Color() : base() { ;} 
     public string Value { get; set; } 
    } 
} 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ComponentDemo._Default" %> 
<%@ Register Assembly="ComponentDemo" Namespace="ComponentDemo" TagPrefix="my" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <my:MyGraph runat="server"> 
      <Colors> 
       <my:Color Value="value1" /> 
       <my:Color Value="value2" /> 
       <my:Color Value="value3" /> 
       <my:Color Value="value4" /> 
      </Colors> 
     </my:MyGraph> 
    </div> 
    </form> 
</body> 
</html> 
관련 문제