开发者

how do you set a url routing routevalue to a hyperlink's navigate url property in an aspx page?

开发者 https://www.devze.com 2023-03-17 05:01 出处:网络
If the following routes: routes.MapPageRoute( \"Build\",// Route name \"build\",// Route URL \"~/build.aspx\" // Web page to handle route

If the following routes:

 routes.MapPageRoute(
           "Build",      // Route name
           "build",      // Route URL
           "~/build.aspx" // Web page to handle route
        );

     routes.MapPageRoute(
       "Type",      // Route name
       "build/{type}",      // Route URL
       "~/build.aspx" // Web page to handle route
    );

       routes.MapPageRoute(
       "Project",      // Route name
       "build/{type}/{project}",      // Route URL
       "~/build.aspx" // Web page to handle route
    );

 routes.MapPageRoute(
       "Task",      // Route name
       "task/{type}/{pro开发者_StackOverflow中文版ject}",      // Route URL
       "~/task.aspx" // Web page to handle route
    );

How do I set a hyperlinks navigate url in the .aspx (not code behind) to the route named 'task' where type = 'tool' and project = 'excel'?


For example, in a grid view template field:

  <asp:TemplateField>
    <ItemTemplate>
      <asp:HyperLink runat="server" ID="btnUp" Text='<%# Eval("ProjectID") %>'
        NavigateUrl='<%# GetRouteUrl("Task",
          new System.Web.Routing.RouteValueDictionary
          {
            {"type", "tool" },
            {"project" , "excel" }
          }) %>' />
    </ItemTemplate>
  </asp:TemplateField>

or, you can set parameters from data source:

  <asp:TemplateField>
    <ItemTemplate>
      <asp:HyperLink runat="server" ID="btnUp" Text='<%# Eval("ProjectID") %>'
        NavigateUrl='<%# GetRouteUrl("Task",
          new System.Web.Routing.RouteValueDictionary
          {
            {"type", Eval("Type") },
            {"project" , Eval("ProjectType") }
          }) %>' />
    </ItemTemplate>
  </asp:TemplateField>


You have to set a RouteName param. E.g. you can set a route like this:

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="<%$RouteUrl:RouteName=Project, type=1, project=2%>">Type 2</asp:HyperLink>


You can set route values like this

<asp:HyperLink ID="lnk3" runat="server" NavigateUrl="<%$RouteUrl:Name=IPhone,ID=1 %>" Text="IPhone" />

In Global.asax my configuration is like this

RouteTable.Routes.MapPageRoute("StoreRoute",
"BookStore/{Name}/{ID}",
"~/Webpages/BookStore/ViewBookDemo.aspx");
0

精彩评论

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