Microsoft KB Archive/309471

= BUG: Init Function in a Visual C# Mobile Web Application Is Not Fired =

Article ID: 309471

Article Last Modified on 8/11/2003

-

APPLIES TO


 * Microsoft Mobile Internet Toolkit 1.0
 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q309471



SYMPTOMS
If you use the Properties window to create an Init event handler in a Visual C# .NET mobile Web application, the code in the handler is not executed because the handler is not called.



CAUSE
In Visual C# code-behind pages, control events are connected to delegates in the InitializeComponent function. The page's Init event executes this function. For the Init event of a child control (which includes all mobile controls, even the mobile Form control) to execute, the Init event must be connected to a delegate with an event handler. A child control's Init event handler must be executed before the page's Init event is executed. Because Microsoft Visual Studio .NET adds code to connect the event to a delegate of a child control's Init event in the InitializeComponent function, the child control's Init event does not execute because the event handler is executed after (and not before) the page's Init event.



RESOLUTION
To work around this problem, change the private modifier for the handler to Protected or Public after you create the handler in the code-behind page. Add a OnInit attribute to the control, and specify the name of the event handler as the value (for example, OnInit=&quot;Form1_Init&quot;).



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



Steps to Reproduce Behavior
 Create a Visual C# Mobile Web application. Add a mobile Web Form to the application named MobileWebForm1. In Design mode, click MobileWebForm1. Right-click on the form, and then click Properties. In the Properties window, click the Events button to switch to Events view.</li> Double-click the Init event for the form. Notice that a form Init function is created in the code-behind file.</li>  Add the following code to the form's Init function: Trace.Write(&quot;form&quot;,&quot;Init&quot;); </li>  In the code-behind file, add the following code to the InitializeComponent function: Trace.Write(&quot;Page&quot;,&quot;InitializeComponent&quot;); </li> Add the Trace=true attribute to the Page directive of the .aspx page to turn on tracing.</li> Compile and view the page in an HTML Web browser. Notice that the code in the Form1_Init method is not executed.</li></ol>

Keywords: kbbug kbide kbpending kbservercontrols KB309471

-

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

© Microsoft Corporation. All rights reserved.