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.

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

<br />

<br />

<br />

<br />

<br />

code snippet PCSDynamicDataDemoEntities/Default.aspx <strong>and</strong> PCSDynamicDataDemoLinq/Default.aspx<br />

A lot of the display code is contained in the master page <strong>and</strong> the CSS file for the web site, which won’t<br />

be listed here to save space. The important section of the preceding code is the GridView control, which<br />

contains a DynamicHyperLink control that renders the link to a table. Data is bound to the GridView<br />

control from code-behind as follows (reformatted slightly for clarity):<br />

protected void Page_Load(object sender, EventArgs e)<br />

{<br />

System.Collections.IList visibleTables =<br />

ASP.global_asax.DefaultModel.VisibleTables;<br />

if (visibleTables.Count == 0)<br />

{<br />

throw new InvalidOperationException(<br />

"There are no accessible tables. Make sure that at least one data"<br />

+ " model is registered in Global.asax <strong>and</strong> scaffolding is enabled"<br />

+ " or implement custom pages.");<br />

}<br />

Menu1.DataSource = visibleTables;<br />

Menu1.DataBind();<br />

}<br />

code snippets PCSDynamicDataDemoEntities/Default.aspx.cs <strong>and</strong> PCSDynamicDataDemoLinq/Default.aspx.cs<br />

This extracts the list of visible tables from the model (in this case, all the tables, because scaffolding is<br />

provided for all of them as discussed earlier), each of which is described by a MetaTable object. The<br />

DynamicHyperLink controls intelligently render links to pages for the tables based on properties of these<br />

objects. For example, the link for the Customers table is as follows:<br />

/PCSDynamicDataDemoLinq/Customers/List.aspx<br />

Obviously, the web site has no such page defined; instead, routing is used as described in the first part of this<br />

chapter to generate content for this link. If you click on the link, you’ll see a listing page for the Customers<br />

table, as shown in Figure 42-5.<br />

figure 42-5<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!