protected void Page_Load(object sender, EventArgs e) { FormView1.DataSource = GetCustomers(); // get data source FormView1.DataBind(); // bind data } private IEnumerableGetCustomers() { // get customer data from database using (var db = new CustomerDataContext()) { return db.Customers.ToList(); } }
protected void btnSave_Click(object sender, EventArgs e) { var customer = new Customer { FirstName = txtFirstName.Text, LastName = txtLastName.Text, Email = txtEmail.Text }; using (var db = new CustomerDataContext()) { db.Customers.InsertOnSubmit(customer); db.SubmitChanges(); } FormView1.DataSource = GetCustomers(); FormView1.DataBind(); }In this example, we are adding a new customer to the database and displaying the updated list of customers on the FormView control. The btnSave_Click event creates a new Customer object with the values entered by the user in the text boxes. It then adds the new customer to the database and retrieves the updated list of customers using the GetCustomers() method. The data is then bound to the FormView control using the DataSource and DataBind methods. Package library: System.Web.UI.WebControls.FormView.