BUG: Init Function in a Visual C# Mobile Web Application Is Not Fired (309471)



The information in this article applies to:

  • Microsoft Mobile Internet Toolkit (MMIT)
  • Microsoft Visual C# .NET (2002)

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="Form1_Init").

STATUS

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

MORE INFORMATION

Steps to Reproduce Behavior

  1. Create a Visual C# Mobile Web application.
  2. Add a mobile Web Form to the application named MobileWebForm1.
  3. In Design mode, click MobileWebForm1.
  4. Right-click on the form, and then click Properties.
  5. In the Properties window, click the Events button to switch to Events view.
  6. Double-click the Init event for the form. Notice that a form Init function is created in the code-behind file.
  7. Add the following code to the form's Init function:
          Trace.Write("form","Init");
    					
  8. In the code-behind file, add the following code to the InitializeComponent function:
          Trace.Write("Page","InitializeComponent");
    					
  9. Add the Trace=true attribute to the Page directive of the .aspx page to turn on tracing.
  10. Compile and view the page in an HTML Web browser. Notice that the code in the Form1_Init method is not executed.

Modification Type:MinorLast Reviewed:8/11/2003
Keywords:kbbug kbide kbpending kbServerControls KB309471