Reputation: 324
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.
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:
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"));
}
Upvotes: 0
Views: 78
Reputation: 765
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,
Upvotes: 1