ASP.NET Click() event doesn't fire on second postback

点点圈 提交于 2019-12-07 17:04:32

问题


I have a ASP.NET web form. The first time I submit the form, the SubmitButton_Click event is raised.

The form is returned to the browser either with validation errors or with the option to submit the form again with new values.

When the form is sumbitted again, the SubmitButton_Click event never fires. Page_Load fires, but not the button.

Does anyone know why I would be seeing this behavior on the server side?

(I am using jQuery and the validation control client-side but I don't think it is causing an issue. Thought I would mention it anyway).

Edit:

<asp:Button ID="SubmitButton" OnClick="SubmitButton_Click"
      runat="server" />

Event is set on the control, not in code.


回答1:


Is the JQuery submitting the form? If __doPostBack() isn't called on the client side (with the approprate arguements), the server side event won't occure.




回答2:


This can happen with dynamically created controls, if you don't create them early enough in the page life cycle. You're a bit short on details, but is your button created dynamically?




回答3:


How are you "wiring up" your event handler?

It sounds like you are wiring it up in a if(IsPostback) block, which is not being run the second time...




回答4:


How is the Click event handler associated with the submit button? Declaratively or in code?

If it's in code, make sure that you're not doing it in a block like this:

if (!Page.IsPostBack) {
    submitButton.Click += new EventHandler(submitButton_Click);
}

This will prevent the event handler from being attached when the page is posted back (i.e. Page.IsPostBack is true).



来源:https://stackoverflow.com/questions/953093/asp-net-click-event-doesnt-fire-on-second-postback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!