2010-05-07 14 views
1

我正在爲DropDownList(也包含標籤)創建複合控件。添加使用DropDownLIst向複合控件提供列表項的能力

想法是我可以像下拉列表一樣使用我的控件,但也可以將標籤放到DDL前面的頁面上。

我對TextBoxes可以很好地工作,但由於Collection(或Datasource)組件填充DDL,所以我在DDL中掙扎。

基本上我希望能夠做這樣的事情:

<ecc:MyDropDownList ID="AnimalType" runat="server" LabelText="this is what will be in the label"> 
<asp:ListItem Text="dog" Value="dog" /> 
<asp:ListItem Text="cat" Value="cat" /> 
</ecc:MyDropDownList> 

的問題是,我不延長將DropDownList類我的控制,所以我不能簡單地與魔術工作,它。我需要一些指針來弄清楚如何將我的控件(MyDropDownList)(當前僅僅是System.Web.UI.UserControl)變成能夠接受標記中的List項的東西,理想情況下,我希望能夠將它插入到數據源中(與常規DDL提供的功能相同)。

我試過沒有運氣,只是擴展了常規的DDL,但無法讓Label組件隨它一起飛。

回答

1

在做了一些挖掘和搜索之後,我發現了一個可行的解決方案。希望這將有助於其他人在未來:

[ParseChildren(true, "Items")] 
public class EDropDownList : CompositeControl, IValidatedFields 
{ 
    public string PromptingText { get; set; } 
    public string Value { get; set; } 
    public Label __Label { get; set; } 
    private ListItemCollection _items; 
    public DropDownList __DropDownList; 
    public ListItemCollection Items 
    { 
     get { return _items; } 
     set 
     { 
      if (_items != value) 
      { 
       _items = value; 
      } 
     } 
    } 

    public string Type { get { return "DropDownList"; } } 


    public EDropDownList() 
    { 
     __Label = new Label(); 
    } 
    protected override void CreateChildControls() 
    { 
     __DropDownList = new DropDownList(); 
     foreach (ListItem myItem in _items) 
     { 
      __DropDownList.Items.Add(myItem); 
     } 
     Controls.AddAt(0, __Label); 
     Controls.AddAt(1, __DropDownList); 
    } 

    protected override void OnLoad(EventArgs e) 
    { 
     // label section    
     __Label.Text = PromptingText+"<br />"; 
     __Label.ForeColor = Color.Red; 
     __Label.Visible = false; 
     // ddl section 
     if (Page.IsPostBack) 
      Value = __DropDownList.SelectedValue; 
    } 
} 
0

最簡單的事情是回到原來的擴展DropDownList控件的選項。你有什麼問題讓標籤與它一起工作?這些問題可能更容易解決?

相關問題