0

I have 2 identical buttons same proprieties, with the difference that 1 is asp button and another one is HTML button, I am trying to redirect to another page, and HTML button works fine, but asp button does not redirect for some reason. I already recreated asp button to see if it is visual studio bug, but it is not. I don't need to use asp controls, but just want to know what i am doing wrong. here is my code example:

HTML Button

<button type="button" id="Btn_16" 
        class="btn btn-block btn-lg btn-danger butao" onclick="see_16()">16 </button>

asp Button

<asp:Button ID="Btn_16" runat="server" Text="16"
      CssClass="btn btn-block btn-lg btn-danger butao" OnClientClick="see_16()" />

DIV like a button (Also works)

<div id="Btn_16" class="btn btn-block btn-lg btn-danger butao" onclick="see_16()">16</div>

All the controls have the same name, but in my code they are commented, so that is not the problem.

see_16 javascript function :

function see_16() {
{
    window.location.href("16.aspx");
};}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • See [how to change pages with JavaScript](http://stackoverflow.com/questions/3846935/how-to-change-page-from-within-javascript). – mason Mar 08 '17 at 17:50
  • @mason -> do try the solution first before commenting on it. OnClientClick="see_16();return false;" worked .. same way as the OnClientClick="return SomeMethod();". – Sailor Mar 08 '17 at 17:56
  • @Sailor He is not changing the page location correctly. – mason Mar 08 '17 at 17:57
  • @mason See the below answer and comments. Solution worked for user which is great. – Sailor Mar 08 '17 at 18:02
  • @Sailor `window.location.href("16.aspx");` is not valid syntax in Google Chrome. Perhaps it works in other browsers, but not Chrome. Depending on the client, it would be quite a big bug if your code doesn't work for the browser that holds 60% of the market share. – mason Mar 08 '17 at 18:05

3 Answers3

1

A server-side ASP.NET Button always calls the postback event. So you need to disable the postback. This works as you want:

OnClientClick="see_16();return false;"

And of course as @ajai Jothi also said you JS should be like this:

window.location.href = "16.aspx";
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
-1

'window.location.href' is not a function

function see_16() {
   window.location.href = "16.aspx";
}
ajai Jothi
  • 2,284
  • 1
  • 8
  • 16
  • 1
    This is only part of the answer, and does not address the postback issue. – mason Mar 08 '17 at 18:06
  • Also you can do a great deal to improve the quality of this answer by explaining things in more detail than, essentially, a code-only answer. – Regular Jo Mar 08 '17 at 23:11
-1

OnClientClick=SomeMethod() event of that BUTTON, it return by default true so after that function it do postback.

for solution use:

//use this code in BUTTON  ==>   OnClientClick="return SomeMethod();"

//and your function like this

<script type="text/javascript">
  function SomeMethod(){
    // put your code here 
    return false;
  }
</script>
Tshilidzi Mudau
  • 7,373
  • 6
  • 36
  • 49
Sailor
  • 183
  • 5