我有一个usercontrol DmsRegisterPod
,它正在一个转发器中输出,转发器本身位于UpdatePanel
内。我在DmsRegisterPod
上有一个名为OnUpdated
的活动,我在转发ItemDataBound
事件中订阅了这个活动,例如:
protected void rptPendingDmsRequests_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DealershipIRLink irLink = (DealershipIRLink)e.Item.DataItem;
DmsRegisterPod dmsRegisterPod = (DmsRegisterPod)e.Item.FindControl("ucDmsRegisterPod");
dmsRegisterPod.ValidationGroup = string.Format("dms-pod-{0}", e.Item.ItemIndex);
dmsRegisterPod.DealershipIRLink = irLink;
dmsRegisterPod.OnUpdated += dmsRegisterPod_OnUpdated;
}
}
private void dmsRegisterPod_OnUpdated(object sender, EventArgs e)
{
this.DataBind();
}
事件在usercontrol中设置:
public event EventHandler OnUpdated;
private void Updated(EventArgs e)
{
if (this.OnUpdated != null)
{
OnUpdated(this, e);
}
}
它是在Accept
点击处理程序中引发的:
protected void btnAccept_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
this.DealershipIRLink.dms_account_number = txtDmsNumber.Text;
this.DealershipIRLink.id_dealer_ir_link_status = DealerIRLinkStatus.DealerIRLinkStatusIdentifier.Approved;
this._irLinkService.UpdateDealershipIRLink(this.DealershipIRLink);
this.Updated(e);
}
}
但是,处理程序OnUpdated
始终为null
,因此事件永远不会被引发。就好像控件以某种方式丢失了事件绑定。谁能看到我在这里做错了什么?
答案 0 :(得分:1)
在ItemCreated
而不是ItemDataBound
中注册事件处理程序,只有在您对控件进行数据绑定而不是每次回发时都会触发该事件处理程序(必需):
protected void rptPendingDmsRequests_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DmsRegisterPod dmsRegisterPod = (DmsRegisterPod)e.Item.FindControl("ucDmsRegisterPod");
dmsRegisterPod.OnUpdated += dmsRegisterPod_OnUpdated;
}
}
依赖于数据源的所有其他逻辑属于ItemDataBound
:
protected void rptPendingDmsRequests_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DealershipIRLink irLink = (DealershipIRLink)e.Item.DataItem;
DmsRegisterPod dmsRegisterPod = (DmsRegisterPod)e.Item.FindControl("ucDmsRegisterPod");
dmsRegisterPod.ValidationGroup = string.Format("dms-pod-{0}", e.Item.ItemIndex);
dmsRegisterPod.DealershipIRLink = irLink;
}
}