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

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

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

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

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

Download Example

Git Repo

Previous articleCall Server-side Function Using jQuery Ajax Call
Next articleBind Asp.net DropDownList Dynamically In C# Vb.net
Hi there, I am Mayank, the man behind Technical Mack. I started AspnetO with a motive to educate people on various programming languages ranging from beginners to expert level. Through this blog, I aim to provide more insightful content.

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

1 × 1 =