简体   繁体   中英

OnClick is not working when i do OnClientClick=“function(); return false”

i have 5 asp:buttons that i want to change css class on when i click them so they are "marked". I do this with javascript and it works fine. But i also want to store a variable in code behind with the onClick.

My problem is, when i click my asp:button it refresh the page. I solved this with ;return false after my OnClientClick. BUT when i have return false, the buttons OnClick doesnt fire. So i need help with getting these both things to work at same time:

  • Page not refreshing when pressing asp:button, same time as
  • OnClick needs to fire also.

html

<asp:Button ID="Button1" runat="server" Text="1" OnClick="button1_Click" OnClientClick="change_select(this); return false;" CssClass="button black"/>

javascript

function change_select(objs) {

    $('.darkgray').removeClass('darkgray').addClass('black');
    objs.className = "button darkgray";

}

code behind

    int[] answerArray = new int[28];
    int answer = 0;
    int currentQ = 0;
 protected void button1_Click(object sender, EventArgs e)
    {         
            answer = 1;        
    }
protected void buttonNext_Click(object sender, EventArgs e)
    {
            answerArray[currentQ] = answer;
            currentQ++;  
    }

You cannot prevent to refresh the page if you have intention to store a value in the code behind using ASP.NET Webforms. See this related SO question How to stop to reload page when click the button?

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