2009-03-23 43 views
13

Tôi biết rằng có thể xác định các thẻ tùy chỉnh trong ASP.NET bằng các điều khiển người dùng. Nhưng theo như tôi biết, bạn chỉ có thể thêm các thuộc tính vào các điều khiển này. Tôi muốn có thể nhúng dữ liệu phức tạp hơn, một chút lite này:Các phần tử tùy chỉnh trong ASP.NET với các phần tử con tùy chỉnh

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

Điều này có thể trong ASP.NET? Tôi có nên thử mở rộng một ListView không? Hoặc nó có một giải pháp tốt hơn và chính xác hơn?

Trả lời

17

Chắc chắn là có thể. Ví dụ bạn các lớp học sẽ như thế nào:

[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; } 
} 

Và đánh dấu thực tế sẽ là:

<myControls:MyGraph id="myGraph1" runat="server"> 
    <Colors> 
    <myControls:Color Value="#abcdef" /> 
    <myControls:Color Value="#123456" /> 
    </Colors> 
</myControls:MyGraph> 
+0

Cảm ơn điều này .. Thật khó với tất cả thuật ngữ liên quan đến điều khiển máy chủ được xây dựng bằng tay để có câu trả lời thẳng. Trong hindsight điều trị các yếu tố bên trong như tài sản và nother khác làm cho rất nhiều ý nghĩa. Chúc mừng! – CResults

0

Bạn không thể sử dụng UserControl cho purpoces như vậy. Như đã đề cập ở trên, kế thừa Kiểm soát hoặc WebControl.

+0

Tại sao bạn có câu trả lời xung đột được đăng? – 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> 
Các vấn đề liên quan