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

偶尔善良 提交于 2019-12-12 18:48:52

问题


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 want 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?


回答1:


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>



回答2:


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.


来源:https://stackoverflow.com/questions/4111110/how-to-use-routing-in-web-forms-and-hyperlinkfields-of-a-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!