对于DataList,通常使用ItemCommand事件,请参考
.aspx
.aspx.cs
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
private void BindData()
{
List
list.Add(new User { Id = 1, Name = "aa" });
list.Add(new User { Id = 2, Name = "bb" });
list.Add(new User { Id = 3, Name = "cc" });
list.Add(new User { Id = 4, Name = "dd" });
DataList1.DataSource = list;
DataList1.DataKeyField = "Id";
DataList1.DataBind();
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "select")
{
lblMsg.Text = (e.Item.FindControl("Label1") as Label).Text;
}
BindData();
}
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
}
在他的“编辑模板”中点击 然后在里面添加按钮