btnSubmit.click not firing when another control does postback

G

Guest

greetings -

I have a btnSubmit button with a Handles btnSubmit.click which works great
if all the user does is click that button.

But, if the user ALSO changes a text box on the page (which has it's own
event and autopostback=true) before clicking submit then it fires the text
box event but never fires the btnSubmit event. (I follow it in the trace).

Surely both event handlers should be fired? Any hints on identifying what I
have done wrong in the code?
 
B

Brock Allen

When you specify AutoPostBack=true, you're asking a postback to happen as
soon as the item has changed. So when the user tabs off of the TextBox then
it fires the client-side onchange event which triggers a postback -- this
happens *before* the user has had a chance to click the button, so thats
why the button's click doesn't fire.

Disable AutoPostBack on the textbox and when they click the button you'll
get both events in one postback to the server.
 
S

Steven Cheng[MSFT]

Thanks for Brock's inputs.

Hi Walesboy,

In addition to what Brock has mentioned, I think you can also have a look
at the different events in asp.net's webform control model:

#Processing Postback Data
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconreceivingpostbackd
atachangednotifications.asp?frame=true

#Capturing Postback Events
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconreceivingpostbacke
ventnotifications.asp?frame=true

What makes the page be submited to serverside is the "PostBack" and
eachtime , there is only one postback event( of a certain postbackable
control) can be fired. Those submit buttons or ImageButtons by default
will cause postback. However, TextBox by default won't cause the page
postback (but it's TextChange event still have effect if the page is
submited by other postback event as long as the its Text is changed).
When TextBox enable AutoPostBack, any textchange at clientside will cause
the page be posted back, but since it is TextBox that cause the postback,
Button's postback event won't be fired.

#Note: TextBox's Change event is not a postback event, it is a data changed
event which are different from each other. You can refer to the above MSDN
reference.

If there is anything unclear, please feel free to post here. Thanks,


Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

Steven / Brock, thanks for your responses, but I'm not convinced that a
fundamental limitation of asp.net is that only one event will be fired. I
have an earlier version of this same app (actually a different project, but
same business solution) running on the production server which DOES work as I
would hope.

When the user changes the quantity text box, and then presses submit, it
BOTH accepts the change to the text box AND carries out the submit (which
updates the database). The text box has AutoPostBack=true.

So I tried promoting my test code to a place on the production server, and
now it ALSO works as I would hope.

So there would seem to be a difference between my local .net/iis version and
the production server such that multiple events ARE fired when the user
initiates them on a page.

Any ideas what versioning to check for this 'fix' to my local?
 
S

Steven Cheng[MSFT]

Hi Walesboy,

Thanks for your response. AS you said that there was a former page in which
you also have a TExtBox (AutoPostBack= true) and a submit button. However,
that page can let us change the Textbox's text and post back the page (with
both TextChanged, Button_Click event be fired) ,yes? I'm a bit surprised
on this since the shouldn't be the expected behavior of the ASP.NET's
webform postback model.

Would you send me a simple repro page? In addition, if you do want to let
the page's button_click also fired when we change a TextBox's text, we can
use the html <input type="text" > element and add some "onchange"
clientside script which will invoke the button's click event at clientside
so as to post back the page. I think this is a normal approach.

Please feel free to let me know if there is anything else you found or
need assistance. Thanks,


Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

Hi Steven -

well finally got time to create a little test sample, and (of course) it
doesn't reproduce the symptom. I can still consistently get it to do BOTH
the txt autopostback and the btnclick at the same post in my full example, so
not sure what is different.

my txt autopostback event is:
Private Sub txtUnitCost_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles txtUnitCost.TextChanged
If Not (IsNumeric(Me.txtQty.Text) And
IsNumeric(Me.txtUnitCost.Text)) Then
OutputNote("Unit Cost must be numeric")
Else
Me.lblTotalCost.Text = FormatCurrency(Me.txtQty.Text *
Me.txtUnitCost.Text, 0, , , )
End If
End Sub

my btn event is:
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSubmit.Click
If ValidatePage() = True Then
'' save the exp
myExp.SaveExp()
If Me.chkStay.Checked = False Then
Response.Redirect("CAPEX4a.aspx")
Else
OutputNote(myExp.ExpDesc + " was successfully added")
End If
End If
End Sub

note that if I change the unit cost, and THEN click the submit button
WITHOUT tabbing out of the txtUnitCost object, it will save the correctly
modified quantity to the database. On my own localhost, however, it will
just sit there and do nothing until I click the btnSubmit a second time.
 
S

Steven Cheng[MSFT]

Hi Walesboy,

Thanks for your followup.
If the thing is just as you mentioned, I think your localhost page's
behavior is the resonable one:
====================
On my own localhost, however, it will
just sit there and do nothing until I click the btnSubmit a second time.
===================

Based on my local test, when there is an AutoPostBack TExtBox and submit
button, when I change the textbox and click the submit button without tab
out the textbox, only the TextChange event will fire(button_click won't
take place). Have you tried testing on some other boxes or create a new
project ?

In addition, what's your production server's asp.net/.net runtime version?
Is it updated to t he latest version with all the .net framework service
pack? I think you can also try using the aspnet_regiis -c command to
reregister the ASP.NET global script files on that server to see whether
this help.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

Thanks Steve - I guess the irony here is that the unexplained action of
firing both events on the server is actually what I want (I believe it's a
more intuitive interface for the user, who doesn't have to sit there
wondering why nothing happened when they clicked submit), so I'm tempted to
let sleeping dogs lie.
 
S

Steven Cheng[MSFT]

Hi walesboy,

Thanks for your reply.
If you do feel that both events fire is the one you like, it's ok you let
that server going though it really make me feel a bit upset. Anyway, if you
meet any further problem or if you decide to modify your page code so as to
avoid that behavior , please feel free to post here. I'll be glad to assist
you :--)

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top