ASP.Net Postback event eats button click event

M

Mark Lingen

I've found a problem with postback event handling and webcontrol
buttons. Try out the following code in an ASP.Net project and you
will see. Create a web project in VB.Net and drop this code ontop of
the webform1:

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
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Label1 As System.Web.UI.WebControls.Label

'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

If Not IsPostBack Then
Me.Label1.Text = "Start..."
Me.TextBox1.Text = ""
End If
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Me.Label1.Text &= "/Text Box Changed"
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
'Response.Redirect("http://www.msn.com")
Me.Label1.Text &= "/Button Clicked"
End Sub
End Class

Results, if the user changes the textbox and then clicks the button
the buttons click event doesn't fire only the textchanged event fires.
My assumption is that both events would fire and the textchanged
event would be first.

Is there a work around to this bug?

Thanks,
Mark Lingen
ICOS Corp
 
D

darrel

Private Sub TextBox1_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Me.Label1.Text &= "/Text Box Changed"
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
'Response.Redirect("http://www.msn.com")
Me.Label1.Text &= "/Button Clicked"
End Sub
End Class

Results, if the user changes the textbox and then clicks the button
the buttons click event doesn't fire only the textchanged event fires.
My assumption is that both events would fire and the textchanged
event would be first.

I'm no expert, but for the TextChanged handler to work, it'd need to be
triggered on the front end via javascript. Is it not doing that? Otherwise,
I'd assume the button click would only be handled by the button click
handler.

-Darrel
 
J

Jos

Mark said:
I've found a problem with postback event handling and webcontrol
buttons. Try out the following code in an ASP.Net project and you
will see. Create a web project in VB.Net and drop this code ontop of
the webform1:

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
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Label1 As System.Web.UI.WebControls.Label

'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

If Not IsPostBack Then
Me.Label1.Text = "Start..."
Me.TextBox1.Text = ""
End If
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Me.Label1.Text &= "/Text Box Changed"
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
'Response.Redirect("http://www.msn.com")
Me.Label1.Text &= "/Button Clicked"
End Sub
End Class

Results, if the user changes the textbox and then clicks the button
the buttons click event doesn't fire only the textchanged event fires.
My assumption is that both events would fire and the textchanged
event would be first.

Is there a work around to this bug?

Thanks,
Mark Lingen
ICOS Corp

I tried it and it works fine.
I get:
Start.../Text Box Changed/Button Clicked

So, there is no bug.

Don't you use any other settings than the code you gave us?
 
M

Mark Lingen

Jos said:
I tried it and it works fine.
I get:
Start.../Text Box Changed/Button Clicked

So, there is no bug.

Don't you use any other settings than the code you gave us?

Did you leave the Focus in the text box after changing it, then click
on the button. Do you still get both events.

Thanks,
Mark
 
G

Guest

I have a very similar problem. Same as Mark I have page with couple of
textboxes and 2 buttons. One of textboxes has Autopostback=true. If textbox
with autopostback=true is empty and I type something into it and click using
my mouse on any of the buttons all events fire as expected Page_Load,
TextChanged, Button.Click. However if textbox is not empty - already contains
some text and I just change this text and click on the button I get only
Page_Load and Textchanged fired, no Button.Click event. Does anyone knows
what is the problem and how to
workaround it?

Thanks
 
Joined
Aug 14, 2008
Messages
1
Reaction score
0
try

I had the same problem on a page in a "ContentPlaceHolder" and it was because i changed the "EnableViewState" property to False

I hope it helps

Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top