Converting Spaces to Hyphens in a URL Using Code-Behind

Joined
Oct 15, 2007
Messages
1
Reaction score
0
Sorry for the long post, but rather than write a vague one and be asked for more information, I thought I'd include the relevant info all in one place.

I have a user control that displays a list of product categories in a web store. The control makes use of a DataList, and the categories are retrieved via a method called "GetCategories", which calls a T-SQL stored procedure. Each category is hyperlinked to a page with the following URL format, where "id" corresponds to "CategoryID" and "name" corresponds to "Name" in my Categories table:

http://www.mywebstore.com/category.aspx?id=3&name=pilsners

While I have successfully displayed the list of categories, I want to format the URLs so that all alphabetic characters are in lowercase and all spaces between words are converted to hyphens. Formatting the URL to be in lowercase was simple, but I'm having trouble converting the spaces to hyphens.

For example:

As it stands now, the links with multi-word category names appear as:

http://www.mywebstore.com/category.aspx?id=5&name=india pale ale

I tried formatting the links with Server.UrlEncode, but that just converts the spaces to plus signs:

http://www.mywebstore.com/category.aspx?id=5&name=india+pale+ale

Then I tried playing with regular expressions, and I think I might be close, but I keep getting a null reference exception. Here's my code:

----------- CategoriesListControl.ascx -----------

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="CategoriesListControl.ascx.vb" Inherits="controls_CategoriesListControl" %>
<asp:DataList ID="listCategories" runat="server">
<ItemTemplate>
<asp:HyperLink ID="linkCategories" runat="server"
NavigateUrl='<%# "../category.aspx?id=" & Eval("CategoryID") & "&name=" & Eval("Name").ToLower %>' Text='<%# Eval("Name") %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:DataList>

----------- CategoriesListControl.ascx.vb -----------

Imports System.Text.RegularExpressions

Partial Class controls_CategoriesListControl
Inherits System.Web.UI.UserControl

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
listCategories.DataSource = CatalogAccess.GetCategories
listCategories.DataBind()
FormatLinks()
End Sub

Public Sub FormatLinks()
Dim link As HyperLink = Me.FindControl("linkCategories")
Dim pattern As String = "\s+"
Dim rgx As New Regex(pattern)
Dim input As String = link.NavigateUrl
Dim output As String = rgx.Replace(input, "-")
End Sub

End Class

The line that's giving me a System.NullReferenceException is:
Dim input As String = link.NavigateUrl

I don't understand why I'm getting the error because I did set "input" equal to something.

I'm not even sure that the method above is an acceptable way to format the links the way I want them. If anyone knows how to convert spaces to hyphens in the same context that I've shown above (i.e., using a DataList), please share it.

Any help with this would be greatly appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top