Я работаю над проектом бюджетирования. Я перечислил доходы в элементе управления списком с вычетами, вложенными в другой элемент управления списком для каждого элемента списка. У меня возникают проблемы при подписке на EventCandler DeductionsChanged. В приведенном ниже примере я пытаюсь заставить действие 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
}
}
}