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



The information in this article 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 "Applies to" section.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Create a Microsoft Visual C# .NET Web Application, and then add a Web Form to the application.
  2. From the Toolbox, drag a Button control onto the Web Form.
  3. 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.
  4. Add the following code in the Button1_Init function:
          Trace.Write("Button","InitThisButton");
    					
  5. 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("Page","InitializeComponent");
    					
  6. In the Web.config file, find the <Trace> element. Make sure that you set the enabled and the pageOutput attributes to true.
  7. 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.
To change the code so that the event fires, follow these steps:
  1. Open the .aspx file, and then click the HTML button.
  2. Add the event handler (oninit) to the button code as shown in the following sample code:
    <asp:Button id="Button1" runat="server" oninit="Button1_Init" Text="Button1"></asp:Button>
    					
  3. 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)
    					
  4. Rebuild and view the .aspx page. Notice that you see the event fire trace.

Modification Type:MinorLast Reviewed:8/26/2005
Keywords:kbvs2002sp1sweep kbbug kbnofix KB314965