Code:
<asp:GridView
ID="gridView"
runat="server"
AutoGenerateColumns="false"
AutoGenerateSelectButton="true"
AllowPaging="true"
DataKeyNames="ID"
DataSourceID="EventDataSource"
SelectedIndex="0">
<Columns>
<asp:TemplateField HeaderText="Activity">
<ItemTemplate>
<asp:Label runat="server" ID="lblEventCategory" Text='<%# Eval("EventCategory.Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="Event" />
<asp:BoundField DataField="LastModifiedDate" HeaderText="Last Modified" HtmlEncode="false" DataFormatString="{0:d}" ReadOnly="true"/>
</Columns>
</asp:GridView>
<asp:detailsview
id="detailsView"
OnItemInserted="detailsView_ItemInserted"
OnItemUpdated="detailsView_ItemUpdated"
datasourceid="EventDetailDataSource"
autogenerateinsertbutton="true"
AutoGenerateEditButton="true"
AutoGenerateDeleteButton="true"
autogeneraterows="false"
datakeynames="ID"
gridlines="Both"
runat="server">
<Fields>
<asp:TemplateField HeaderText="Activity">
<ItemTemplate>
<asp:Label runat="server" ID="lblEventCategory" Text='<%# Eval("EventCategory.Name") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="lstEventCategoryEdit" SelectedValue='<%# Bind("EventCategoryID") %>' DataValueField="ID" DataTextField="Name" DataSourceID="EventCategoryDataSource" runat="server">
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="lstEventCategoryInsert" SelectedValue='<%# Bind("EventCategoryID") %>' DataSourceID="EventCategoryDataSource"
DataValueField="ID" DataTextField="Name" runat="server">
</asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="Event" />
<asp:BoundField DataField="LastModifiedDate" ReadOnly="true" InsertVisible="false" HeaderText="Last Modified" />
</Fields>
</asp:detailsview>
<asp:ObjectDataSource
ID="EventDataSource"
runat="server"
TypeName="BusinessFacade.EventFacade"
DataObjectTypeName="Business.Event"
SelectMethod="GetAll">
</asp:ObjectDataSource>
<asp:ObjectDataSource
ID="EventDetailDataSource"
runat="server"
SelectMethod="GetById"
InsertMethod="Save"
UpdateMethod="Save"
DeleteMethod="Delete"
TypeName="BusinessFacade.EventFacade"
DataObjectTypeName="Business.Event">
<SelectParameters>
<asp:ControlParameter ControlID="gridView" Type="Int32" Name="ID" PropertyName="SelectedValue" />
</SelectParameters>
<InsertParameters>
<asp:ControlParameter ControlID="lstEventCategoryInsert" PropertyName="SelectedValue" Name="EventCategoryID" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="LastModifiedDate" Type="DateTime" />
</UpdateParameters>
<DeleteParameters>
<asp:ControlParameter ControlID="gridView" Type="Int32" Name="ID" PropertyName="SelectedValue" />
</DeleteParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="EventCategoryDataSource" runat="server" SelectMethod="GetAll"
TypeName="BusinessFacade.EventCategoryFacade"></asp:ObjectDataSource>
public class EventFacade : BusinessFacadeBase
{
public EventFacade()
{
}
public void Save(Event entity)
{
BaseDataAccess mgr = new BaseDataAccess();
CheckObjectBeforeSave(entity);
mgr.Save(entity);
}
public void Delete(Event entity)
{
BaseDataAccess mgr = new BaseDataAccess();
mgr.Delete(entity);
}
public IList GetAll()
{
BaseDataAccess mgr = new BaseDataAccess();
return mgr.Get(typeof(Event));
}
public Event GetById(int id)
{
BaseDataAccess mgr = new BaseDataAccess();
return mgr.Get(typeof(Event), id) as Event;
}
}
The DataAccess layer and NHibernate session management is implemented as
http://blog.benday.com/archive/2005/03/16/198.aspx
Edit: I added code tags to preserve layout -- Sergey