2014年7月13日 星期日

[RESOLVED] VS 2010 : How to build a Route using $RouteUrl


Hi,


I already know I can use < % $RouteUrl:code=ALFKI % > to create a route url to access ALFKI client (northwind database sample).


Now I would like to user $RouteUrl to create an HyperlinkColumn in a gridview. I tried NavigateURL="< %$RouteUrl:code=eval("customerid") % >"" but this doesn't work. So, is it possible to use $RouteURL to create route urls in a column of a gridview ?


 


Thank you !


 


Dennes



I believe there are some limitations in HyperLinkColumn and this is one of them.


Why you don't use a TemplateColumn and place a hyperlink control inside it ?




details







The following code worked for me. Note that in my case, the hyperlink is in within a datalist control and not gridview control, but I think it will equally work in a gridview. Using one of the overloads of GetRouteUrl did the job. In my case, I needed four
values from the database to be built into the url of the hyperlink.




<%# ((System.Data.Common.DbDataRecord)Container.DataItem)["PlaceName"] %>



The .aspx file containing the above code must have the System.Web.Routing namespace reference on top.


<%@ Import Namespace="System.Web.Routing" %>

Also Note that I added the following code to my global.asax file


void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("ChosenPlaceDetails",
"{stateregion}/{city}/{categoryname}/{placename}",
"~/placedetails.aspx");
}

I did not use Eval("StateRegion") method to retrieve database values as this method is highly discouraged due performance issues associated with its use (google "the evils of Eval"). Instead, I used the much faster code below since my data is coming from a datareader object.


((System.Data.Common.DbDataRecord)Container.DataItem)["StateRegion"]

Hope this helps those who find themselves with a similar situation.








沒有留言:

張貼留言