user710502
user710502

Reputation: 11471

Redirection not happening when clicking on button

I am trying to redirect the user to a different page upon clicking on a button and stop any asp related events if it meets the validation in the if() statement in javascript, the reason why i am doing it like this is because on a specific page.. it is not grabinhg the On_Click event for some reason. While debugging it.. before it goes into the On_Click event in the .cs file it call multiple classes and functions .. by the time it is done.. it is redirecting somewhere else and never goes through the On_Click event.. now these are a dozen of classes it is going through so I decided to use javascript to do it right after the button is clicked without letting it evaluate in .cs BUT i still want to allow that for other pages because it is working there.. this page seems to be special..

When i click on the image button it is supposed to check the function first and then if its true redirect and stop any other processing on Page_Load or anywhere but its not working see my code here in my user control

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="FindPage.ascx.cs"
    Inherits="Data_Manager_FindPage" %>
<script type="text/javascript">
    function ForwardSearch() {
        if (window.location.href.indexOf('FindPage.aspx') > -1) {
            var value = document.getElementById('<%= txtFind.ClientID %>').value;
            window.location.href = '~/ResultSet.aspx?findvalue=' + encodeURIComponent(value);
        }
    }

</script> 

Upvotes: 0

Views: 250

Answers (1)

Denny Ferrassoli
Denny Ferrassoli

Reputation: 1725

One issue is that your ForwardSearch() JavaScript function does not explicitly return true / false.

When you add "OnClientClick='return ForwardSearch();'" it will use the result of the function to determine if it should continue executing. Since your ForwardSearch() doesn't return anything it defaults to return true and therefore it will continue executing and eventually call your server-side btnFind_Click.

try:

<script type="text/javascript">
function ForwardSearch() {
    if (window.location.href.indexOf('FindPage.aspx') > -1) {
        var value = document.getElementById('<%= txtFind.ClientID %>').value;
        window.location.href = '~/ResultSet.aspx?findvalue=' + encodeURIComponent(value);
        return false;
    }
    return true;
}

Upvotes: 1

Related Questions