简体   繁体   English

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

[英]web forms dynamic dropdownlist tooltips

I have cascading dropdowns one is for topics and the other for sections. 我有级联的下拉菜单,一个用于主题,另一个用于部分。 I was hoping to be able to use tooltips to show the description of each topic and section. 我希望能够使用工具提示来显示每个主题和部分的描述。 However I first have to choose a particular topic and or a section to get the tooltip to show up and also the only description that shows up is for the one at the bottom of the dropdown regardless if its selected or not. 但是,我首先必须选择一个特定的主题和/或一个部分以显示工具提示,并且显示的唯一说明是针对下拉列表底部的内容,无论是否选中它。 Any ideas what I'm doing wrong? 有什么想法我做错了吗?

Below is how I'm loading the topic dropdown. 以下是我如何加载主题下拉菜单。 Load_Topic1() is being called on the Page_Load method. 在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"));
    }

Here are my cascading dropdowns: 这是我的级联下拉列表:

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

The 2nd dropdown or section1 dropdown is being given its information from this method: 通过此方法可以向第二个下拉菜单或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"));
    }

You adding the attribute just for the dropdown, not for each element in the dropdown. 您仅为下拉列表而不是下拉列表中的每个元素添加属性。

What you need to do is: 您需要做的是:

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

        }

And of course the same for section. 当然,该节也是如此。 This should give each select element in your option and title. 这应该为您的选项和标题中的每个选择元素。

Cheers, 干杯,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM