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" %>
<aspataList 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>
</aspataList>
----------- 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.
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" %>
<aspataList 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>
</aspataList>
----------- 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.