Source First:
Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2.Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Bars" runat="server">
<ItemTemplate>
<div>
<asp:TextBox ID="Name" Text='<%# Eval("Name") %>' runat="server" AutoPostBack="true" />
<asp:TextBox ID="Description" Text='<%# Eval("Description") %>' runat="server" AutoPostBack="true" />
</div>
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="AddBar" Text="Add Bar" runat="server" onclick="AddBar_Click" />
</div>
</form>
</body>
</html>
Default.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication2
{
public class Foo
{
public string Widget { get; set; }
public string Cog { get; set; }
public List<Bar> Bars { get; set; }
}
public class Bar
{
public string Name { get; set; }
public string Description { get; set; }
}
public partial class Default : System.Web.UI.Page
{
public Foo Foo
{
get
{
var retval = Session["Foo"];
if (retval == null)
{
retval = new Foo();
Session["Foo"] = retval;
}
return retval == null ? new Foo() : (Foo)retval;
}
set
{
Session["Foo"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var f = new Foo() { Bars = new List<Bar>(), Cog = "Cog one", Widget = "Widget Master" };
f.Bars.Add(new Bar() { Name = "Bar one", Description = "hello." });
Foo = f;
Bind();
}
}
protected void Bars_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item != null && e.Item.DataItem != null)
{
var b = e.Item.DataItem as Bar;
var Name = e.Item.FindControl("Name") as TextBox;
var Description = e.Item.FindControl("Description") as TextBox;
Name.TextChanged += new EventHandler(Name_TextChanged);
}
}
protected void Name_TextChanged(object sender, EventArgs e)
{
var Name = sender as TextBox;
var listItem = Name.Parent as RepeaterItem;
var f = Foo;
var b = f.Bars[listItem.ItemIndex];
b.Name = Name.Text;
Foo = f;
Bind();
}
protected void AddBar_Click(object sender, EventArgs e)
{
var f = Foo;
f.Bars.Add(new Bar());
Foo = f;
Bind();
}
private void Bind()
{
Bars.ItemDataBound += new RepeaterItemEventHandler(Bars_ItemDataBound);
Bars.DataSource = Foo.Bars;
Bars.DataBind();
}
}
}
Now, I haven’t used databinding in years but so I thought I’d refresh my memory, but I can’t get this to work. When I start the app, I see one entry with the values I expect. If I click add, new values show up and the existing value is preserved.
Here is the problem: When I change the value in the Name textbox, I can see the page refresh, but my Name_TextChanged event doesn’t break. Since it doesn’t fire, the value goes back to what it was before.
Attach OnTextChanged event in aspx. The following codes work.