Partial Public Class SalesOrderHeader
End Class
Public Class SalesOrderHeaderMetadata
<DisplayFormat(DataFormatString:="{0:dd-MMM-yyyy}",
ApplyFormatInEditMode:=True)> _
<UIHint("DateCalendar")> _
Public OrderDate As Object
<DisplayFormat(DataFormatString:="{0:dd-MMM-yyyy}",
ApplyFormatInEditMode:=True)> _
<UIHint("DateCalendar")> _
Public DueDate As Object
<DisplayFormat(DataFormatString:="{0:dd-MMM-yyyy}",
ApplyFormatInEditMode:=True)> _
<UIHint("DateCalendar")> _
Public ShipDate As Object
End Class
Code snippet SalesOrderHeader.vb
506 .
chaPter 23 dynAmic dATA
Figure 23-11 shows the custom field template in the Edit view of an entry in the SalesOrderHeader
table.
fiGure 23-11
entity templates
Entity templates are used to render the user interface for an individual entry from a table. The default
entity templates are stored in the DynamicData\EntityTemplates folder, and include templates to
create, edit, and display a record. These templates work with the default page templates, and render
the UI using a two-column HTML table; label in the left column, data field in the right.
Customizing the existing entity templates affects all tables. You can also create a new custom entity
template for a specific table. This allows you to provide a completely different layout when editing
an entry from a database table compared to when the entry is simply viewed.
To create a new entity template, right-click the DynamicData\EntityTemplate folder and select
Add . New Item. Choose a new Web User Control and name it SalesOrderHeaders.ascx.
The default templates use an EntityTemplate control, which is more or less equivalent to a Repeater
web server control. This control dynamically generates all of the fields for this table from the data
model. In this case, instead of using an EntityTemplate control, you can manually specify the fields
to be displayed. The following code lists a custom markup for the entity template that displays a
subset of the data:
<tr>
<td class="DDLightHeader">
<asp:Label ID=”Label1” runat="server" Text="Customer" />
</td>
Customizing the Presentation .
507
<td>
Acct No:
<asp:DynamicControl ID=”DynamicControl1” runat="server"
DataField="AccountNumber" />
<br/>
PO No:
<asp:DynamicControl ID=”DynamicControl2” runat="server"
DataField="PurchaseOrderNumber" />
</td>
</tr>
<tr>
<td class="DDLightHeader">
<asp:Label ID=”Label2” runat="server" Text="Dates" />
</td>
<td>
Ordered:
<asp:DynamicControl ID=”DynamicControl3” runat="server"
DataField="OrderDate" />
<br/>
Due:
<asp:DynamicControl ID=”DynamicControl4” runat="server"
DataField="DueDate" />
<br/>
Shipped:
<asp:DynamicControl ID=”DynamicControl5” runat="server"
DataField="ShipDate" />
</td>
</tr>
<tr>
<td class="DDLightHeader">
<asp:Label ID=”Label3” runat="server" Text="Amount" />
</td>
<td>
Sub Total:
<asp:DynamicControl ID=”DynamicControl6” runat="server"
DataField="SubTotal" DataFormatString="{0:c}" />
<br/>
Tax:
<asp:DynamicControl ID=”DynamicControl7” runat="server"
DataField="TaxAmt" DataFormatString="{0:c}" />
<br/>
Freight:
<asp:DynamicControl ID=”DynamicControl8” runat="server"
DataField="Freight" DataFormatString="{0:c}" />
</td>
</tr>
Code snippet DynamicData\EntityTemplates\SalesOrderHeader.ascx
Finally, change the web user control to inherit from System.Web.DynamicData.EntityTemplate
UserControl instead of System.Web.UI.UserControl:
508 .
chaPter 23 dynAmic dATA
c#
public partial class SalesOrderHeaders :
System.Web.DynamicData.EntityTemplateUserControl
Code snippet DynamicData\EntityTemplates\SalesOrderHeader.ascx.cs
Vb
Public Class SalesOrderHeaders
Inherits System.Web.DynamicData.EntityTemplateUserControl
Code snippet DynamicData\EntityTemplates\SalesOrderHeader.ascx.vb
You can now build and run the project to test the new entity template. Figure 23-12 shows the
default entity template (left) and the new customized template (right) for the SalesOrderHeader
table. The Edit and Insert views are unchanged, because the read-only Details template was the only
template that was customized.
fiGure 23-12
Customizing the Presentation .
509
filter templates
Filter templates are used to display a control that filters the rows that are displayed for a table.
Dynamic Data ships with three filter templates, stored in the DynamicData\Filters folder.
These filters have self-explanatory names — the Boolean filter is used for Boolean data types, the
Enumeration filter is used when the data type is mapped to an enum, and the ForeignKey filter is
used for foreign key relationships.
Figure 23-13 shows the four filter templates that are rendered by default for the SalesOrderHeader
table. The first filter, OnlineOrderFlag, is a Boolean filter and only contains three options — All,
True, and False. The remaining three filters are generated from foreign keys, and each has a large
number of entries.
fiGure 23-13
You may have noticed that the values displayed in the Customer drop-down list
are simply the customer’s title (Mr, Mrs, and so on), which are next to useless.
To select the field that is displayed for foreign keys, Dynamic Data finds the first
field on the table with a string type. This can be overridden to any other field on
the table by decorating the data model class with a DisplayColumnattribute.
However, in the case of the Customer table what you really want is to display a
string containing a number of fields (FirstName, LastName). To do this, simply
override the ToString method of the Customer data model class.
Unfortunately, drop-down lists are only useful if they contain fewer than a couple of hundred
entries. Anything more than this and the rendering of the web page will slow down and the list will
be difficult to navigate. As the number of customers in the database grows to thousands, or more,
510 .
chaPter 23 dynAmic dATA
the use of a drop-down list for the Address, Address1, and Customer foreign keys will render this
page unusable.
If you wanted to keep these filters, you could do something advanced such as customize the default
ForeignKey filter with a search control that performed a server callback and displayed a list of valid
entries that matched the search, all within an AJAX request of course! However, such an exercise
is well beyond the scope of this book, so instead you can learn how to control which fields are
rendered as filters.
The remainder of this section assumes you have created a custom page template
for the SalesOrderDetail table, as described earlier in this chapter.
Open the custom List.aspx template for the SalesOrderHeader table from DynamicData\
CustomPages\SalesOrderHeaders. Locate the QueryableFilterRepeater control on this page. This
control is used to dynamically generate the list of filters. Delete this control, and in its place add a
DynamicFilter control as shown in the following code. The DataField property must be set to the
correct data field for the filter, and the FilterUIHint property should be set to the correct filter
template.
Online Order:
< asp:DynamicFilter ID=”OnlineOrderFilter” runat="server"
DataField="OnlineOrderFlag" FilterUIHint="Boolean"
OnFilterChanged="DynamicFilter_FilterChanged" >
< /asp:DynamicFilter >
Code snippet DynamicData\CustomPages\SalesOrderHeader\List.aspx
Next, locate the QueryExtender control toward the bottom of the page. This control is used to
“wire up” the DynamicFilter control to the data source, so that the correct query will be used when
the filter changes. Modify the ControlID property to match the name of the DynamicFilter control
you just added, as shown in the following code:
< asp:QueryExtender TargetControlID=”GridDataSource” ID=”GridQueryExtender”
runat="server" >
< asp:DynamicFilterExpression ControlID=”OnlineOrderFilter” / >
< /asp:QueryExtender >
Code snippet DynamicData\CustomPages\SalesOrderHeader\List.aspx
Finally, you will need to remove some code that was only required by the QueryableFilterRepeater
control. Open the code - behind file ( List.aspx.cs or List.aspx.vb) and remove the Label_PreRender
method. When you save the changes and run the project, you will see only a single filter displayed for the
SalesOrderHeader table, as shown in Figure 23 - 14.
enabling Dynamic Data for existing Projects .
511
fiGure 23-14
enablinG dynaMic data for existinG ProJects
Dynamic Data is undoubtedly a very powerful way to create a new data-driven web application
from scratch. However, with the version of Dynamic Data that ships with Visual Studio 2010, you
can use some of the features of Dynamic Data in an existing Web Application or Web Site project.
The EnableDynamicData extension method has been introduced to enable this functionality.
This method can be called on any class that implements the System.Web.UI.INamingContainer
interface. This includes the Repeater, DataGrid, DataList, CheckBoxList, ChangePassword,
LoginView, Menu, SiteMapNodeItem, and RadioButtonList controls.
Adding this functionality to an existing web control does not require the application to be using
LINQ to SQL or the Entity Framework. In fact, the application could be using any data access
option including plain old ADO.NET. This is because the Dynamic Data functionality that is
enabled in this way does not include any of the scaffolding functionality. Instead, it enables both
field templates and the validation and display attributes that were described earlier in this chapter.
For example, to enable Dynamic Data on a GridView control, call the EnableDynamicData
extension method as shown in the following code:
c#
GridView1.EnableDynamicData(typeof(Product));
Vb
GridView1.EnableDynamicData(GetType(Product))
You can now create a Product class with public properties that match the data displayed
in GridView1. Each of these properties can be decorated with attributes from the System
.ComponentModel.DataAnnotations namespace, such as Required, StringLength,
512 .
chaPter 23 dynAmic dATA
RegularExpression, or DisplayFormat. ASP.NET will interpret these attributes at run time and
automatically apply the relevant validations and formatting.
This allows any application to leverage Dynamic Data without making any significant changes to
the application.
suMMary
In this chapter you learned how to use ASP.NET Dynamic Data to create a data-driven web
application with little or no code. More importantly, you also learned how flexible Dynamic Data is
by customizing the data model and web pages.
By freeing developers from needing to write reams of low-level data access code, Dynamic Data
allows for faster development time, and lets your developers build features that add more value to
end users.
24
sharePoint
what’s in this chaPter?
.
Setting up a development environment for SharePoint
.
Developing custom SharePoint components such as Web Parts,
lists, and workflows
.
Debugging and testing SharePoint projects
.
Packaging and deploying SharePoint components
Over the past couple of years the level of interest — and number of deployments — in
Microsoft SharePoint has reached the point where SharePoint is now one of Microsoft’s fastest
growing product lines.
SharePoint is really a collection of related products and technologies that broadly service the
areas of document and content management, web-based collaboration, and search. SharePoint
is also a very flexible application hosting platform, which allows you to develop and deploy
everything from individual Web Parts to full-blown web applications.
Although it can be used to host web sites for anonymous external visitors, SharePoint is much
more ideally suited for web sites that involve registered users, particularly those that service
the needs of employees within an organization. SharePoint provides much of the low-level
integration code that is often required in these environments including built-in authentication
and authorization, integration with Microsoft Office, access to external data, provisioning of
sites, and collaborative workflow.
This chapter runs through the SharePoint development tools in Visual Studio 2010, and
demonstrates how to build, debug, and deploy SharePoint solutions.
514 .
chaPter 24 ShArepoinT
In addition to using Visual Studio 2010, you can create SharePoint solutions
using the free SharePoint Designer 2010. SharePoint Designer provides a very
different implementation approach by presenting the elements of a SharePoint
solution in a high-level logical way that hides many of the underlying
implementation details. It also includes some excellent WYSIWYG tools to
browse and edit components in existing SharePoint sites. As such, SharePoint
Designer is often considered the tool of choice for non-developers (IT
Professionals and end-users). However, it is still useful to developers as certain
development and configuration tasks, such as building page layouts and master
pages, are much easier to perform using SharePoint Designer. Typically, you’ll
find more experienced SharePoint developers using both tools to provision their