I have a problem in a complex bit of code. It appears that a delegate method call is interrupting the click event of a dynamically added button. I haven’t coded up a basic example to be 100% sure but it looks like that’s what’s happening.
We have a .aspx page which contains a user control
<wachter:ViewEditTemplateControl ID="veSiteInfo" runat="server" Mode="View" EnableAjax="false" CanEditPermission="SiteInfoCanEdit">
<ViewButtonsTemplate>
<asp:ImageButton ID="ibMap" CommandName="Map" ImageUrl="../Images/NavIcons/map.png" Visible="true" ToolTip="Site Map" runat="server" />
<asp:ImageButton ID="ibtnSiteNotes" CommandName="SiteNotes" ImageUrl="/Images/iconInfo.png" visible="false" ToolTip="Site Notes" runat="server" />
<asp:ImageButton ID="imgEdit" CommandName="Edit" ToolTip="Edit" runat="server" ImageUrl="/Images/iconEdit.png" />
</ViewButtonsTemplate>
<ViewTemplate>
<uc1:SiteInformationView ID="SiteInformationView1" runat="server" />
</ViewTemplate>
<EditTemplate>
<uc1:SiteInformationEdit ID="SiteInformationEdit1" runat="server" />
</EditTemplate>
</wachter:ViewEditTemplateControl>
In the .aspx’s codebehind we set a delegate to the ViewTemplate’s usercontrol
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
(veSiteInfo.FindControl("SiteInformationView1") as JobSiteViewControl).SiteChanged += SiteChanged;
}
This delegate is used to set the visibility of our edit button imgEdit.
The ViewEditTemplateControl contains a bit of code in the codebehind which dymaically adds save/edit buttons
Button btnSave = new Button();
btnSave.ID = "btnSave";
btnSave.Text = "Save";
btnSave.CommandName = "Save";
root.Controls.Add(btnSave);
Button btnCancel = new Button();
btnCancel.ID = "btnCancel";
btnCancel.Text = "Cancel";
btnCancel.CommandName = "Cancel";
btnCancel.CausesValidation = false;
root.Controls.Add(btnCancel);
In the ViewTemplate’s codebehind we call this delegate on page init.
public Action<JobSiteEntity> SiteChanged;
public override void InitControl()
{
if (SiteChanged != null)
SiteChanged(DataItemTyped);
}
The problem is that clicking the dynamically added btnCancel triggers the delegate but doesn’t fire the event handler for it.
If I comment out setting the delegate, the button’s event handler gets hit.
I know my code snippets aren’t the best but any ideas would be appreciated.
Not the best solution, but I’ve ended up removing the delegate call and directly accessing a method on the base page.