简体   繁体   English

我执行OnClientClick =“ function();时,OnClick无法正常工作; 返回假”

[英]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". 我有5个asp:button,当我单击它们时要更改css类,因此它们被“标记”了。 I do this with javascript and it works fine. 我使用javascript执行此操作,效果很好。 But i also want to store a variable in code behind with the onClick. 但我也想在onClick后面的代码中存储一个变量。

My problem is, when i click my asp:button it refresh the page. 我的问题是,当我单击asp:button时,它将刷新页面。 I solved this with ;return false after my OnClientClick. 我在OnClientClick之后用; return false来解决了这个问题。 BUT when i have return false, the buttons OnClick doesnt fire. 但是当我返回false时,OnClick按钮不会触发。 So i need help with getting these both things to work at same time: 因此,我需要帮助使这两种功能同时起作用:

  • Page not refreshing when pressing asp:button, same time as 按下as:按钮时页面未刷新,与
  • OnClick needs to fire also. OnClick也需要触发。

html html

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

javascript 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. 如果您打算使用ASP.NET Webforms在后面的代码中存储值,则不能阻止刷新页面。 See this related SO question How to stop to reload page when click the button? 看到这个相关的SO问题如何在单击按钮时停止重新加载页面?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM