如何使用动态添加的用户控件注册事件?

时间:2016-09-09 15:22:50

标签: asp.net

我有以下用户控制 CustomerControl.ascx ):

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CustomerControl.ascx.cs" Inherits="CustomerControl" %>

<tr>
    <td><%= this.FirstName %></td>
    <td><%= this.LastName %></td>
    <td><%= this.Email %></td>
    <td><%= this.Service %></td>
    <td><%= this.Amount %></td>
    <td>
        <button ID="DelCustomerBtn" OnServerClick="OnDelBtnClicked" runat="server">Delete</button>
    </td>
</tr>

它是一个带有删除按钮的表格行,该按钮反映了Customers表格IdFirstNameLastNameEmail,{{ 1}},Service)。 代码隐藏文件为( Amount ):

CustomerControl.ascx.cs

首页 protected void Page_Load(object sender, EventArgs e) { // Add data-id = id_of_current_row_in_database DelCustomerBtn.Attributes.Add("data-id", this.Id.ToString()); } private int id; private string firstName; private string lastName; private string email; private string service; private decimal amount; public event EventHandler DelBtnClicked; protected void OnDelBtnClicked(object sender, EventArgs e) { if (DelBtnClicked != null) DelBtnClicked(this, e); } public int Id { get { return id; } set { id = value; } } public string FirstName { get { return firstName; } set { firstName = value; } } public string LastName { get { return lastName; } set { lastName = value; } } public string Email { get { return email; } set { email = value; } } public string Service { get { return service; } set { service = value; } } public decimal Amount { get { return amount; } set { amount = value; } } )中包含以下内容:

Default.aspx

最后,其代码隐藏文件( <table> <thead> <tr> <th>First</th> <th>Last</th> <th>Email</th> <th>Service</th> <th>Amount</th> <th></th> </tr> </thead> <tbody> <asp:PlaceHolder ID="RowPlaceholder" runat="server" /> </tbody> </table> )包含:

Default.aspx.cs

这个

背后的想法

protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // Retrieve records from the DB: CustomersDataContext dataContext = new CustomersDataContext(); var users = from user in dataContext.Customers select new { Id = user.Id, FirstName = user.FirstName, LastName = user.LastName, Email = user.Email, Service = user.Service, Amount = user.Amount }; // Declare CustomerControl object to hold controls: CustomerControl CustomerControl; // Add CustomerControl controls for each record in the DB: foreach (var user in users) { // Load the CustomerControlcontrol: CustomerControl = (CustomerControl)LoadControl("~/CustomerControl.ascx"); // Set the control's data: CustomerControl.Id = user.Id; CustomerControl.FirstName = user.FirstName; CustomerControl.LastName = user.LastName; CustomerControl.Email = user.Email; CustomerControl.Service = user.Service; CustomerControl.Amount = user.Amount; CustomerControl.DelBtnClicked += HandleDelBtnClicked; // Add the control to RowPlaceholder: RowPlaceholder.Controls.Add(CustomerControl); } } // endif } public void HandleDelBtnClicked(object sender, EventArgs e) { Button btn = (Button)sender; // Retrieve id_of_current_row_in_database, as set in user control int id = int.Parse(btn.Attributes["data-id"].ToString()); CustomersDataContext dataContext = new CustomersDataContext(); var find = from cst in dataContext.Customers where cst.Id == id select cst; dataContext.Customers.DeleteOnSubmit(find.First()); dataContext.SubmitChanges(); } 的{​​{1}}属性中的每条记录的ID存储在ID为data-id的{​​{1}}属性中。检索button的值以便按id查找记录(将Linq引用到SQL)并删除它。

问题

这段代码虽然超级简单,但不起作用。当我单击“删除”按钮时,页面将刷新,我不再在DelCustomerBtn中看到任何行。没有数据从数据库中删除。

我在这里做错了什么?

0 个答案:

没有答案
相关问题