1

我正在为 DropDownList(还包括一个标签)创建一个复合控件。

我的想法是我可以像下拉列表一样使用我的控件,但也可以让它在 DDL 前面的页面上扔一个标签。

我可以完美地为 TextBoxes 工作,但是由于要填充 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一个与常规 DDL 提供的功能相同)。

我只是尝试扩展常规 DDL,但没有成功,但无法让Label组件随它一起飞行。

4

2 回答 2

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;
    }
}
于 2010-05-07T20:04:12.503 回答
0

最简单的方法是返回到扩展 DropDownList 控件的原始选项。您在使用标签时遇到了什么问题?这些问题可能更容易解决?

于 2010-05-07T14:30:52.530 回答