-->

OnClick events not working in ASP.NET page

2019-04-02 17:57发布

问题:

I am having a aspx page which inherits from a master page. In the master page I have a button that is common for every page in the solution.

<div class="btn_general_mid">
<asp:Button 
    ID="btnMainSearch" 
    ValidationGroup="MainSearch" 
    OnClientClick="CheckSearchTextBox()"
    CausesValidation="true" 
    runat="server" 
    OnClick="btnMainSearch_Click"  
    CssClass="search_btn_submit"
    Text="Search" />
</div>

Here the CheckSearchTextBox() is a javascript function and the btnMainSearch_Click is the event which is handling the code behind part of the button.

In a certain page this button click event btnMainSearch_Click is not fired. (The debugger does not reach that in the code)

In the runtime, (When examined using Firebug) this is the code segment generated for the button.

<div class="btn_general_mid">
<input id="ctl00_btnMainSearch" 
class="search_btn_submit" 
type="submit"    
onclick="CheckSearchTextBox();WebForm_DoPostBackWithOptions(new  WebForm_PostBackOptions("ctl00$btnMainSearch", "", true, "MainSearch", "", false, false))" 
value="Search" 
name="ctl00$btnMainSearch"></div>

Actually in the other pages where this button works fine , the same html output is generated for this button. Am I missing something here?

回答1:

Check whether there are any validation errors because it would not allow server side event btnMainSearch_Click getting fired.



回答2:

One reason for this is may be that postback is being stopped by some visible or apparently invisible validator. Try adding cause validation to false in button tag.

CausesValidation="false"