1
votes

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++;  
    }
1
You want to trigger server side event handlers without refreshing the page?jbabey
make the function return true or false... & use e.preventdefaultloxxy
ajax is best suited for what u r trying to accomplsihiJade

1 Answers

0
votes

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?