ASP .NET Button event handlers do not fire on the first click, but on the second click after a PostBack

后端 未结 8 2058
Happy的楠姐
Happy的楠姐 2020-12-02 17:02

Background: I am customizing an existing ASP .NET / C# application. It has it\'s own little \"framework\" and conventions for developers to follow when exte

8条回答
  •  佛祖请我去吃肉
    2020-12-02 17:35

    I had the same problem, but the accepted answer here was not causing it. I had a text box and a search button, and clicking the button the first time didn't perform the search. The event handler of the button wasn't being hit. But clicking the button a second time did trigger the event on the server. Here is why:

    If you have an with its AutoPostBack set to true, after typing in the text box and then moving to click a button, the text box causes a post-back immediately the moment it loses focus. So the click even of the button doesn't count (the page is already posted-back as a result of the text box's event). That's why when you click the button a second time, it works because the text box is not involved in the second post-back.

    Set the AutoPostBackproperty of the to false to fix this issue.

提交回复
热议问题