Web表单动态下拉列表工具提示

时间:2018-11-07 13:13:39

标签: c# asp.net drop-down-menu webforms

我有级联的下拉菜单,一个用于主题,另一个用于部分。我希望能够使用工具提示来显示每个主题和部分的描述。但是,我首先必须选择一个特定的主题和/或一个部分以显示工具提示,并且显示的唯一说明是针对下拉列表底部的内容,无论是否选中它。有什么想法我做错了吗?

以下是我如何加载主题下拉菜单。在Page_Load方法上调用Load_Topic1()。

protected void Load_Topic1()
    {
        var topics = ReadTopics();

        foreach (var topic in topics)
        {
            var topicListItem = new ListItem(topic.Name, topic.Id.ToString());
            topic1.Items.Add(topicListItem);
            topic1.Attributes.Add("Title",topic.Description);
        }

        topic1.Items.Insert(0, new ListItem("--- Select Topic ---", "0"));
    }

这是我的级联下拉列表:

<asp:UpdatePanel ID="updatePanel1" runat="server">
                        <ContentTemplate>
                            <asp:DropDownList ID="topic1" DataTextField="NAME" DataValueField="ID" OnSelectedIndexChanged="Load_Section1" AutoPostBack="True" AppendDataBoundItems="true" runat="server"/>
                            <asp:DropDownList ID="section1" DataTextField="NAME" DataValueFile="ID"  runat="server">
                                <asp:ListItem Text="--- Select Section ---" Value="0"></asp:ListItem>
                            </asp:DropDownList><br/>
                            <asp:RequiredFieldValidator runat="server" ID="topic1ReqVal" InitialValue="0" ControlToValidate="topic1" errormessage="Please select a topic"/>
                            <asp:RequiredFieldValidator runat="server" ID="section1ReqVal" InitialValue="0" ControlToValidate="section1" errormessage="Please select a section"/><br/>
                            </ContentTemplate>
                    </asp:UpdatePanel>

正在通过此方法为第二个下拉菜单或section1下拉菜单提供信息:

 protected void Load_Section1(object sender, EventArgs e)
    {
        section1.Items.Clear();

        var sections = ReadForTopic(Guid.Parse(topic1.SelectedValue));

        foreach (var section in sections)
        {
            var sectionListItem = new ListItem(section.Name, section.Id.ToString());
            section1.Items.Add(sectionListItem);
            section1.Attributes.Add("Title", section.Description);
        }

        section1.Items.Insert(0, new ListItem("--- Select Section ---", "0"));
    }

1 个答案:

答案 0 :(得分:1)

您仅为下拉列表而不是下拉列表中的每个元素添加属性。

您需要做的是:

        foreach (var topic in topics)
        {
            var topicListItem = new ListItem(topic.Name, topic.Id.ToString());
            topicListItem.Attributes.Add("Title",topic.Description);
            topic1.Items.Add(topicListItem);

        }

当然也与本节相同。这应该在您的选项和标题中给出每个选择元素。

干杯

相关问题