Confirm postback OnClientClick button ASP.NET

后端 未结 10 1303
梦毁少年i
梦毁少年i 2020-12-01 15:47


        
10条回答
  •  温柔的废话
    2020-12-01 15:59

    There are solutions here that will work, but I don't see anyone explaining what is actually happening here, so even though this is 2 years old I'll explain it.

    There is nothing "wrong" with the onclientclick javascript you are adding. The problem is that asp.net is adding it's on onclick stuff to run AFTER whatever code you put in there runs.

    So for example this ASPX:

    
    

    is turned into this HTML when rendered:

    
    

    If you look closely, the __doPostBack stuff will never be reached, because the "confirm" will always return true/false before __doPostBack is reached.

    This is why you need to have the confirm only return false and not return when the value is true. Technically, it doesn't matter if it returns true or false, any return in this instance would have the effect of preventing the __doPostBack from being called, but for convention I would leave it so that it returns false when false and does nothing for true.

提交回复
热议问题