Today i was writing a program, and I ran into a problem where I had DropDownLists drawing data from the database, but the text was really long, thus making the DDL’s stretch halfway accross the page.  After doing a little searching I was able to simply solve it by adding a little code.

Basically what this does is:  in the pre_render section of the dropdownlist control, you

1. Add the “title” attribute to each list item (regardless of size), this way they all have tooltips

2. Scroll through the list items to see how many characters we have

3. If they’re greater than 95, grab the left 95 characters of text, add an elipses

Protected Sub ddlProgramSession6_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlProgramSession6.PreRender

For Each li In ddlProgramSession6.Items

li.Attributes.Add(“title”, li.Text)
If li.Text.Length > 95 Then
Dim strTemp As String
strTemp = Left(li.Text, 95) & “…”
li.Text = strTemp
End If

Next
End Sub

This was an easy way to solve this problem.  Maybe in the future I’ll try to make the elipses into a hyperlink so it’s more intuitive to the user that there is more text.

Advertisements