父页面无法订阅嵌套的子控件事件处理程序的问题

问题描述 投票:0回答:1

我正在做一个预算项目。我已经在listview控件中列出了收入,并为每个收入listview项在另一个listview控件中嵌套了扣除项。我在订阅DeductionsChanged EventHandler时遇到麻烦。对于下面的示例,我试图获取delete动作来触发DeductionsChanged事件,以便可以在父页面上调用BindIncome();但是DeductionsUpdated始终为null,并且永远不会触发ucDeductionsEditor_DeductionsUpdated。

我在这里想念什么?

用户控制

namespace BillMoney.UserControls.BillMoneyUserControls
{
    public partial class IncomeDeductionsEditor : BillMoney.App_Code.BaseUserControl
    {
        public EventHandler DeductionsUpdated;
        public virtual void OnDeductionsUpdated(EventArgs e)
        {
            DeductionsUpdated?.Invoke(this, e); // This is always null even though it is subscribed to in lvIncome_ItemDataBound 
        }

    public Int32 Invalue_IncomeId
    {
        get { return (ViewState["Invalue_IncomeId"] != null) ? (Int32)ViewState["Invalue_IncomeId"] : 0; }
        set
        {
            ViewState["Invalue_IncomeId"] = value;
            BindDeductions();
        }
    }

        ...

        protected void lvDeductions_ItemDeleting(object sender, ListViewDeleteEventArgs e)
        {
            ListView lvDeductions = (ListView)sender;
            Int32 deductionid = DataTypeMapper.GPC_CInt(lvDeductions.DataKeys[e.ItemIndex].Value);
            DeductionMGR.Delete(deductionid);
            OnDeductionsUpdated(null); // This DOES fire when I click the delete button
            BindDeductions();
        }

       ...

        private void BindDeductions()
        {
            List<DeductionDTO> deductions = new List<DeductionDTO>();
            deductions = DeductionMGR.GetList(Invalue_IncomeId);
            lvDeductions.DataSource = deductions;
            lvDeductions.DataBind();
        }
    }
}

父页面标记

<asp:ListView ID="lvIncome" DataKeyNames="IncomeId" runat="server" OnItemDataBound="lvIncome_ItemDataBound">
            <LayoutTemplate>
                <div id="itemplaceholder" runat="server"></div>
            </LayoutTemplate>
            <ItemTemplate>
                <div class="w3-row content-bubble">
                    <h3><b>
                        <asp:HyperLink ID="hlIncomeSource" Text="" NavigateUrl="#" Target="_blank" runat="server"></asp:HyperLink></b></h3>
                    <div class="row">
                        <div class="w3-col s3 l3 text-small text-bold">Start Date</div>
                        <div class="w3-col s3 l3 text-small text-bold">Pay Frequency</div>
                        <div class="w3-col s3 l3 text-small text-bold">Next Pay Date</div>
                        <div class="w3-col s3 l3 text-small text-bold w3-right-align">Income</div>
                    </div>
                    <div class="row">
                        <div class="w3-col s3 l3">
                            <asp:Label ID="lblStartDate" runat="server" CssClass="text-small"></asp:Label></div>
                        <div class="w3-col s3 l3">
                            <asp:Label ID="lblPayFrequency" runat="server" CssClass="text-small"></asp:Label></div>
                        <div class="w3-col s3 l3">
                            <asp:Label ID="lblNextPayDate" runat="server" CssClass="text-small"></asp:Label></div>
                        <div class="w3-col s3 l3 w3-right-align">
                            <asp:Label ID="lblGrossIncome" runat="server" CssClass="text-small"></asp:Label></div>
                    </div>
                    <div class="row w3-right-align">
                        <div class="w3-col l12 border-bottom">
                            <UC:DeductionsEditor ID="ucDeductionsEditor" OnDeductionsUpdated="ucDeductionsEditor_DeductionsUpdated" runat="server" />
                        </div>
                    </div>
                    <div class="row w3-right-align">
                        <div class="w3-col l12">
                            <asp:Label ID="lblTotalIncome" runat="server"></asp:Label>
                        </div>
                    </div>
                </div>
            </ItemTemplate>
        </asp:ListView>

后面的父页面代码

namespace BillMoney.BillMoneyPages.Income
{
    public partial class income_index : BillMoney.App_Code.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ...

            if (!Page.IsPostBack)
            {

                BindIncome();
            }
        }

        private void BindIncome()
        {
            // Gross Income
            List<IncomeDTO> income_list = new List<IncomeDTO>();
            income_list = IncomeMGR.GetList(ActiveUser.UserId);
            lvIncome.DataSource = income_list;
            lvIncome.DataBind();   
        }

        ...

        protected void lvIncome_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                ListViewDataItem item = (ListViewDataItem)e.Item;
                IncomeDTO income = (IncomeDTO)item.DataItem;

                ...

                IncomeDeductionsEditor ucDeductionsEditor = (IncomeDeductionsEditor)item.FindControl("ucDeductionsEditor");
                ucDeductionsEditor.Invalue_IncomeId = income.IncomeId; // lvDeductions gets bound when Invalue_IncomeId is set
                ucDeductionsEditor.DeductionsUpdated += ucDeductionsEditor_DeductionsUpdated;
            }
        }

        private void ucDeductionsEditor_DeductionsUpdated(object sender, EventArgs e)
        {
            BindIncome(); // This never fires when I click the Delete button on the USERCONTROL
        }
    }
}
eventhandler
1个回答
0
投票
protected void lvIncome_ItemCreated(object sender, ListViewItemEventArgs e) { if (e.Item.ItemType == ListViewItemType.DataItem) { ListViewDataItem item = (ListViewDataItem)e.Item; IncomeDeductionsEditor ucDeductionsEditor = (IncomeDeductionsEditor)item.FindControl("ucDeductionsEditor"); ucDeductionsEditor.DeductionsUpdated += ucDeductionsEditor_DeductionsUpdated; } }
© www.soinside.com 2019 - 2024. All rights reserved.