Here is a working example, that uses the ObejctDataSource:
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["Mode"] == "Insert")
FormView1.DefaultMode = FormViewMode.Insert;
else
FormView1.DefaultMode = FormViewMode.ReadOnly;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FormView DataSourceID="ObjectDataSource1"
ID="FormView1" runat="server">
<InsertItemTemplate>
CustomerName:
<asp:TextBox ID="CustomerNameTextBox" runat="server" Text='<%# Bind("CustomerName") %>'></asp:TextBox><br />
CustomerTitle:
<asp:DropDownList SelectedValue='<%# Bind("CustomerTitle") %>' DataSourceID="ObjectDataSource1" ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetAllTitles"
TypeName="CustomerRepository"></asp:ObjectDataSource>
<br />
<asp:LinkButton CausesValidation="True" CommandName="Insert" ID="InsertButton" runat="server"
Text="Insert"></asp:LinkButton>
<asp:LinkButton CausesValidation="False" CommandName="Cancel" ID="InsertCancelButton"
runat="server" Text="Cancel"></asp:LinkButton>
</InsertItemTemplate>
<ItemTemplate>
CustomerName:
<asp:Label ID="CustomerNameLabel" runat="server" Text='<%# Eval("CustomerName") %>'>
</asp:Label>
<br />
CustomerID:
<asp:Label ID="CustomerIDLabel" runat="server" Text='<%# Eval("CustomerID") %>'>
</asp:Label>
<br />
CustomerTitle:
<asp:Label ID="CustomerTitleLabel" runat="server" Text='<%# Eval("CustomerTitle") %>'>
</asp:Label>
<br />
<asp:LinkButton CausesValidation="False" CommandName="New" ID="InsertButton" runat="server"
Text="Insert">
</asp:LinkButton>
</ItemTemplate>
</asp:FormView>
<asp:ObjectDataSource DataObjectTypeName="Customer" ID="ObjectDataSource1" runat="server"
SelectMethod="GetAll"
TypeName="CustomerRepository"
InsertMethod="Insert"></asp:ObjectDataSource>
</div>
</form>
</body>
</html>
CustomerRespository.cs
public class CustomerRepository
{
public CustomerRepository()
{
}
public IList<Customer> GetAll()
{
IList<Customer> customers = new List<Customer>();
customers.Add(new Customer(1, "Fredrik", "Developer"));
customers.Add(new Customer(2, "Johan", "Developer"));
customers.Add(new Customer(3, "Lovisa", "Product Manager"));
customers.Add(new Customer(4, "Anna", "Program Manager"));
return customers;
}
public void Insert(Customer customer)
{
}
public void Insert(string customerName, string customerTitle)
{
}
public string[] GetAllTitles()
{
return new string[] { "Developer", "Product Manager", "Program Manager" };
}
}
Customer.cs
public class Customer
{
private int _customerId;
private string _customerName;
private string _customerTitle;
public Customer()
{
}
public Customer(int customerId, string customerName, string customerTitle)
{
this._customerId = customerId;
this._customerName = customerName;
this.CustomerTitle = customerTitle;
}
public int CustomerID
{
get { return this._customerId; }
internal set { this._customerId = value; }
}
public string CustomerName
{
get { return this._customerName; }
set { this._customerName = value; }
}
public string CustomerTitle
{
get { return this._customerTitle; }
set { this._customerTitle = value; }
}
}
/Fredrik Normén -
fredrikn @ twitterASPInsider
Microsoft MVP, MCSD, MCAD, MCT
ASPInsidersMy Blog