简体   繁体   中英

PostBack OnTextChanged is triggering when unrelated button is clicked

On my page, I have:

  • txtCalendar , an asp:TextBox used to enter a date, with an attached JQuery datepicker
  • btnSave , an asp:Button to save the form
  • btnAdd , an asp:LinkButton that opens a new form for a different part of the page.

When either button is clicked, txtCalendar_TextChanged is called before the btnSave_Click or btnAdd_Click function is called. txtCalendar_TextChanged shouldn't be called at all when these buttons are clicked - before or after.

It is possible that the JQuery datepicker is somehow causing this - when I comment out the code instantiating datepicker and binding it to the textbox, txtCalendar_TextChanged is not called.

Additionally, I have verified:

  • Even when I prevent the $(function() {} script from running after a postback, the txtCalendar_TextChanged method is still called when a button is clicked
  • I do not have any JavaScript that looks for the ID #btnSave or #btnAdd or anything that could match/respond to those IDs
  • the sender for txtCalendar_TextChanged is my txtCalendar textbox
  • eventargs for txtCalendar_TextChanged is empty (not sure if this is significant)
  • the text in txtCalendar doesn't actually change (verified visually in the UI, not by adding code on the backend to check)
  • txtCalendar_TextChanged is not called when I remove OnTextChanged='txtCalendar_TextChanged' (so - it is being called by txtCalendar , somehow...)
  • txtCalendar_TextChanged is called even when I remove AutoPostBack='true'
  • txtCalendar_TextChanged is called even when I disable the btnSave_Click function by commenting out Handles btnSave.Click (both buttons use Handles btnX.Click in the aspx.vb rather than OnClick="btnX_Click" in the aspx)
  • Page_Load is hit before txtCalendar_TextChanged , but the first check is If(Page.IsPostBack) Then Exit Sub - the rest of Page_Load doesn't run (so - other backend functions are not being hit)

Solved:

In vb.net, I use myDate.ToShortDateString to set the initial value in txtCalendar . ToShortDateString formats dates without leading zeroes, while datepicker 's default format does have leading zeroes

When datepicker is instantiated, it changes the date format in txtCalendar , but since this is done via code, it doesn't trigger a postback. When the user clicks a button and a postback is triggered, txtCalendar_TextChanged is also triggered because of the new date format (the text string has changed).

Changing datepicker 's dateFormat to m/d/yy fixed the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM