开发者

How to use routing in web forms and HyperLinkFields of a GridView

I have a route mapped like this:

 routes.MapPageRoute("section-page-id", "{section}/{page}/{id}", "~/{section}/{page}.aspx")

I have a GridView with a HyperLinkField that use to look like this:

<asp:HyperLinkField NavigateUrl="<%$RouteUrl:RouteName=section-page-id,section=Clients,page=Groups,id=5%>" Text="Groups" />

This works fine; however, I wa开发者_StackOverflow社区nt to have the 5 as the dynamic Id of the record in the GridView. Before I tried routing, I would use DataNavigateUrlFields but I cannot get it working here. Any suggestions?


The asp hyperlink control will not work, but a simple anchor will! I tried tons of different solutions with server side code and referencing controls, but this appears to be the only solution I have found with a gridview and routing in asp.net 4.0 web forms.

<asp:TemplateField>
  <ItemTemplate>
    <a href="<% #GetRouteUrl("section-page-id",
      new with  {.section="Clients", .page="Groups", .id=Eval("Id")})%>">test</a>
  </ItemTemplate>
</asp:TemplateField>


Convert it to a template field then use the Eval function to grab the ID field. Renaming "ID" to whatever your Group ID field is called.

<asp:HyperLinkField NavigateUrl="<%$RouteUrl:RouteName=section-page-id,section=Clients,page=Groups,id=Convert.Int32(Eval("ID"))%>" Text="Groups" />

You may need to tweak this a little.

Alternatively use the OnRowDataBound event to set the NavigateUrl of your Hyperlink.

var hyperLink = (HyperLink)e.Row.FindControl("HyperLink1");
hyperLink.NavigateUrl = ""; //Set here.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜