Home Asp.net Bind Asp.net DropDownList Title or Tooltip From Code-behind

Bind Asp.net DropDownList Title or Tooltip From Code-behind

0
3

In my previous tutorials, I’d explained about country-state-city cascading for dropdownlist, dynamically bind and show data in dropdownlist, main differences between executereader executenonquery and executescalar and other more cracking tutorials on DropDownList, GridView, Asp.net here.

Now here in this tutorial, I’ll explain how you can bind asp.net dropdownlist title or tooltip to it’s list items using c# as well as vb.net with example code.

I guess you all know about how to bind data to asp.net dropdownlist, so I’m skipping that part and going to explain how you can show tooltip to asp.net dropdown list items.

Define OnDataBound Event of DropDownList – [.aspx]

To show title or tooltip, you need to add OnDataBound event of dropdownlist. Here is the sample dropdownlist with OnDataBound event:

<asp:DropDownList ID=”ddlSubjects” runat=”server” OnDataBound=”ddlSubjects_OnDataBound”>
</asp:DropDownList>

After adding OnDataBound event to dropdown, choose your language (that is .cs or .vb) and add the following code to your code-behind file.

Bind Asp.net DropDownList Title or Tooltip Using OnDataBound Event – [C#]

protected void ddlSubjects_OnDataBound(object sender, EventArgs e)
{
DropDownList dd = sender as DropDownList;
if (dd != null)
{
foreach (ListItem li in dd.Items)
{
li.Attributes[“title”] = li.Text;
}
}
}

Bind Asp.net DropDownList Title or Tooltip Using OnDataBound Event – [Vb]

Protected Sub ddlSubjects_OnDataBound(ByVal sender As Object, ByVal e As EventArgs)
Dim dd As DropDownList = TryCast(sender, DropDownList)
If dd IsNot Nothing Then
For Each li As ListItem In dd.Items
li.Attributes(“title”) = li.Text
Next li
End If
End Sub

Example Result

How to Show Title or Tooltip in Asp.net DropDownList using C# or Vb.net?

Download Example

[wpdm_file id=29]

Git Repo

3 COMMENTS

  1. Your post is not working that items of dropdownlist are creating manually in asp.x page. So what do i?
    (How to show title or tooltip in asp .net dropdownlist using c# or vb)

      • Hi kekiz, This post is specific to add “title” attribute from code-behind file using c# or vb. You can also use “Title” attribute or property from .aspx page (I guess you did it!) when your data are static.

        Thanks for visiting and posting your valuable comments. 🙂

LEAVE A REPLY

Please enter your comment!
Please enter your name here
Captcha verification failed!
CAPTCHA user score failed. Please contact us!