Microsoft KB Archive/314965

= BUG: Imperatively Wired Control Event Does Not Fire in Visual C# .NET Web Application =

Article ID: 314965

Article Last Modified on 8/26/2005

-

APPLIES TO


 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2003 Professional Edition

-



This article was previously published under Q314965



SYMPTOMS
A control event on a Microsoft Visual C# .NET Web application form may not fire. The event does not fire if you wire the control event imperatively, that is, if you double-click the event from the Events view and then add the code.



RESOLUTION
Add the event to the HTML code behind the form, that is, wire the control event declaratively.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section.



Steps to Reproduce the Behavior
 Create a Microsoft Visual C# .NET Web Application, and then add a Web Form to the application. From the Toolbox, drag a Button control onto the Web Form. In the Properties window, click the button that resembles a lightning-bolt to switch to the Events view, and then double-click the Init property for the button. This positions the insertion point on the Button1_Init function in the .aspx.cs file.  Add the following code in the Button1_Init function: Trace.Write(&quot;Button&quot;,&quot;InitThisButton&quot;);   Add the following line of code as the last line in the InitializeComponent function (just above the Button1_Init function in the .aspx.cs file): Trace.Write(&quot;Page&quot;,&quot;InitializeComponent&quot;);  In the Web.config file, find the <Trace> element. Make sure that you set the enabled and the pageOutput attributes to true.</li> Compile and view the .aspx page in the Internet browser.

Alternatively, to view the page, right-click the .aspx file in Solution Explorer, and then click View in Browser.

Notice that Button1_Init function is never called because the trace does not list the InitThisButton string. However, the InitializeComponent function does fire and is listed in the trace as InitializeComponent string. The InitializeComponent function contains the event handler for the button and should fire.</li></ol>

To change the code so that the event fires, follow these steps: <ol> Open the .aspx file, and then click the HTML button.</li>  Add the event handler (oninit) to the button code as shown in the following sample code: <asp:Button id=&quot;Button1&quot; runat=&quot;server&quot; oninit=&quot;Button1_Init&quot; Text=&quot;Button1&quot;></asp:Button> </li>  Make sure that the name of the event handler matches the name in the C# code for the form, and then change the event handler in the C# code to public as follows: public void Button1_Init(object sender, System.EventArgs e)                   </li> Rebuild and view the .aspx page. Notice that you see the event fire trace.</li></ol>

Keywords: kbbug kbnofix kbvs2002sp1sweep KB314965

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.