15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

1250 ❘ ChaPTer 42 Asp.net dynAmic dAtA And mvc<br />

Alternatively, you can extract parameters by name as they are indexed in the collection; for example:<br />

string parameterValue = Page.RouteData.Values["pathparameter"] as string;<br />

expression Builders<br />

There are two expression builders that you can use in ASP.<strong>NET</strong> markup files to extract <strong>and</strong> use route<br />

parameter values: RouteValue <strong>and</strong> RouteUrl.<br />

RouteValue can be used inline to output the value of a parameter, as follows:<br />

<br />

For example, in the PCSRoutingDemo web site, the Target.aspx file includes the following line of markup code:<br />

targetparameter = <br />

code snippet PCSRoutingDemo/Target.aspx<br />

This sets the Text property of a Label control to the value of the targetparameter parameter.<br />

RouteUrl is used to construct URLs to match a route. This is a great feature when you are putting links in<br />

your ASP.<strong>NET</strong> code, as a URL created in this way will change if the route definition ever changes. To use<br />

this expression builder, you use markup inline as follows:<br />

<br />

You can include more than one parameter by separating name/value pairs with commas. The expression<br />

builder will attempt to match the parameters you specify to a route by the names of the parameters you use.<br />

In some cases, you might have more than one route that uses the same parameters, in which case you can<br />

identify the specific route to use by name as follows:<br />

<br />

In PCSRoutingDemo, there are two separate links in the master page, one of which uses this expression builder:<br />

Navigation with routing<br />

Navigation with routing <strong>and</strong> expression builder<br />

code snippet PCSRoutingDemo/MasterPage.master<br />

The second version of this link shows the best way to include links in your markup, as it allows you to<br />

change the route specification later, as noted above.<br />

Data Query Parameters<br />

Another way to use route parameters in your code is to supply them directly to data queries. This technique<br />

enables you to fetch data from a database directly from markup code, rather than having to extract<br />

parameter values <strong>and</strong> use them in the code-behind.<br />

To do this, you use an in the parameters for a query. For example, you could use<br />

the following in an data source:<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!