添加使用 DropDownLIst 向复合控件提供列表项的功能

发布于 2024-08-31 19:41:29 字数 669 浏览 4 评论 0原文

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

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

我的这个功能非常适合文本框,但由于要填充 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 组件随之运行。

I'm creating a composite control for a DropDownList (that also includes a Label).

The idea being that I can use my control like a dropdown list, but also have it toss a Label onto the page in front of the DDL.

I have this working perfectly for TextBoxes, but am struggling with the DDL because of the Collection (or Datasource) component to populate the DDL.

Basically I want to be able to do something like this:

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

The problem is, I'm not extending the DropDownList class for my control, so I can't simply work it with that magic. I need some pointers to figure out how I can turn my control (MyDropDownList), which is currently just a System.Web.UI.UserControl, into something that will accept List items within the tag and ideally, I'd like to be able to plug it into a datasource (the same functions that the regular DDL offers).

I tried with no luck just extending the regular DDL, but couldn't get the Label component to fly with it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感性不性感 2024-09-07 19:41:29

经过一些挖掘和搜索后,我找到了一个可行的解决方案。希望这能帮助其他人在未来:

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

After doing some digging and searching I found a solution that works. Hopefully this will help someone else out in the future:

[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;
    }
}
抠脚大汉 2024-09-07 19:41:29

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

The easiest thing would be to go back to your original option of extending the DropDownList control. What problems did you have getting the label to work with it? Those problems are probably easier to solve?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文