Losing Events in code-behind
Have you been working days, weeks months on a web app when all of a sudden button clicks stop working and you cannot figure out why? I'm not sure why this is happening but it is very frustrating.
I've run into it all the time on a project that is using VSS for source control. We're not sure what I happening but the Initialize() event will, 50% of the time, lose all the wire-up code when we check out a page. To make sure it does not happen what we did was add a WireupEvents() call like this:
1
2#region Web Form Designer generated code
3override protected void OnInit(EventArgs e)
4{
5 //
6 // CODEGEN: This call is required by the ASP.NET Web Form Designer.
7 //
8 InitializeComponent();
9 WireupEvents(); // combat VSS bug...
10 base.OnInit(e);
11}
12
13/// <summary>
14/// Required method for Designer support - do not modify
15/// the contents of this method with the code editor.
16/// </summary>
17private void InitializeComponent()
18{
19 this.Load += new System.EventHandler(this.Page_Load);
20}
21/// <summary>
22/// Combats the VSS bug for losing our events
23/// </summary>
24private void WireupEvents()
25{
26 this.findEntryGroup.Click += new System.EventHandler(this.findEntryGroup_Click);
27 this.doAction.Click += new System.EventHandler(this.doAction_Click);
28}
29#endregion
30
Since we implemented this method of doing things we have not lost a single wired up event.