Init event does not fire

  • Thread starter Thread starter J.Marsch
  • Start date Start date
J

J.Marsch

I must be missing something here: The Init event for controls does not seem
to fire.

What I did:
Drop a textbox on a blank webform, hook the textbox's Init event.
Code:
this.Textbox1.Value = "Hello World";

The textbox comes up blank, and if I set a breakpoint on that line of code,
I find that it never fires. What am I doing wrong here?
 
Hi,

In order to set textbox value you should drop that line inside server
side event:

private void Page_Load(object sender, System.EventArgs e)
{
this.TextBox1.Text = "ff";
}

Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377
 
Thanks, I can live with that. But I am still very curious. Why doesn't the
init event fire? Is that bug? Was I using it incorrectly? If so, how is
it intended to be used?
 
Hi,

I have done more research regarding this issue. It should be a problem of
our product. The init event fires in VB.NET but it does not fire in
C#..NET. Please try the following simple code in VB.NET.
-----------------------------------------------
Public Class WebForm1
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub

Private Sub TextBox1_Init(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.Init
TextBox1.Text = "test inint"
End Sub
End Class
------------------------------------

I will report this issue to the product team. Thank you very much for
providing this important information. If there are any more concerns for
this issue, please feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Jacob Hi,

I don’t think it’s a bug. As you know the ASPX file eventually
translated into *cs file and compiled into DLL when the page being
called. The initialization of the object take place in the ASPX derived
DLL. ASPX.NET run the ASPX derived DLL first and just then the
application DLL run. so if you will try to catch control init event in
the ASPX file (using server side script) you will catch it but trying to
catch that event on the code behind will failed because the control
already been initialize.

Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377
 
Natty, it looks like a bug to me, because the behavior is completely
different based only upon which language you choose. (works in VB, but not
in C#)

Try this:
1. Create a VB.Net Webforms project

2. Drop a Textbox control on the default webform

3. Go to the codeview (the code-behind .vb file, not aspx) and add an event
handler for the textbox's Init event

4. Put this code in the init event: me.TextBox1.Text = "Hello World"

5. Run

The web page will display exactly what you would expect: a text box
containing "Hello World".

Now try exactly the same procedure, except in C# (adjust the syntax, of
course). The textbox will be empty, and the Init event will never have
fired.
 
Thank you for following up on this, Jacob. I appreciate your hard work.
Are there any plans to release a service pack for VS.Net?
 
Hi J,

I have reported this issue but I have not gotten any responses from the
product team now.

As a support engineer, I cannot promise when there will be a service pack
for the VS.NET and conform if this issue is a bug or by design. Thank you
for your understanding.

If there is any update from the product team, I will post it in this thread
as soon as possible.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi J,

I have done more research regarding this issue and found a useful Knowledge
Base artilcle for this issue. Please refer to it carefully.

BUG: Imperatively Wired Control Event Does Not Fire in Visual C# .NET Web
Application
http://support.microsoft.com/?id=314965
"...
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 at the beginning of this article.
..."

Does it answer your question? If I have misunderstood your concern, please
feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top