绑定中继器中项目上的事件不会触发
本文关键字:事件 中继器 项目 绑定 | 更新日期: 2023-09-27 18:14:22
来源:
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();
}
}
}
现在,我已经好几年没有使用数据绑定了,但是我想我会刷新我的记忆,但是我不能让这个工作。当我启动应用程序时,我看到一个具有我期望的值的条目。如果单击add,将显示新值,并保留现有值。
这是问题:当我改变名称文本框中的值时,我可以看到页面刷新,但我的Name_TextChanged事件没有中断。
在aspx中附加OnTextChanged事件。下面的代码可以工作。
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;
// Commented
// Name.TextChanged += new EventHandler(Name_TextChanged);
}
}
<asp:TextBox ID="Name" Text='<%# Eval("Name") %>' runat="server"
AutoPostBack="true" OnTextChanged="Name_TextChanged" />
<asp:TextBox ID="Description" Text='<%# Eval("Description") %>'
runat="server" AutoPostBack="true" OnTextChanged="Name_TextChanged" />