Introduction
While browsing The Code Project, I ran across an article by micahbowerbank which provided a control for alphanavigation. I thought this was great!!! A client had requested a feature just like this. My initial idea was to use a simple DataGrid
and handle everything myself... but dragging and dropping this control sounded too good to be true. I downloaded the control and tried to create a demo project to use it. Well immediately, I saw that the control seemed to need to open a connection and then it wasn't closed. I tried closing it and it bombed. I read a little into the messages following the control and decided it might be a good opportunity to learn about making your own custom controls. Waaalaa... here is my creation, really not much of micahbowerbank's creation is left... maybe small references here and there... and there are a lot of functionalities that you may decide to add to the control...however I laid out what I feel are the base parts of the control for you. Please be kind in your reviews as this is my first control ever!
Using the Code
The following is an example of the code needed to use the control. First reference the AlphaNavigator.dll, then add it to your Toolbox. Next drag a new instance onto the page and in the properties/events panel, set the name for the ItemCommand
event.
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
anUserBind()
End If
End Sub
Private Sub anUserBind()
Dim SQL_COMMAND As String
SQL_COMMAND = "SELECT DISTINCT SUBSTRING(UserName,1,1) "
SQL_COMMAND += "FROM USERS ORDER BY SUBSTRING(UserName,1,1)"
Dim myConnection As New OleDbConnection("SQL CONNECTION STRING HERE")
Dim myCommand As New OleDbCommand(SQL_COMMAND, myConnection)
Dim myAdapter As New OleDbDataAdapter(myCommand)
Dim myDS As New DataSet
myAdapter.Fill(myDS)
AlphaNavigator1.DataSource = myDS.Tables(0).DefaultView
AlphaNavigator1.DataBind()
End Sub
Private Sub anClick(ByVal sender As System.Object, ByVal e As _
System.Web.UI.WebControls.CommandEventArgs) _
Handles AlphaNavigator1.ItemCommand
Dim Letter As String = e.CommandArgument
End Sub
The Control Itself
The control itself ends up being a lot more complex than the original version. If you have the knowledge of making controls, what I've done should make some sense, I hope?? If you notice something I did wrong, I would really appreciate it if you could email me and let me know as this control was built as a learning experience and I'd like to know the mistakes I made.
One area I had difficulty with was getting the link buttons inside the control to use the same font that was set on the base control, I had to set them...but I have a feeling this could be accomplished using inheritance from the main class.
Another thing, the way it works right now you can't set the size of the spaces between each letter. I was thinking about adding that feature but for now this will do. Good Luck!