0

Possible Duplicate:
prevent postback of HtmlButton in C#

Here's my JavaScript function:

<script type = "text/javascript">
    function CheckForEmptySearchBox(id) {
        var index = id.substring(id.length - 1);
        var boxContent = document.getElementById("contentMain__lvTSEntry__txtClientName_" + index).value;
        if (boxContent == "") {
            alert("Please enter search criteria");
            return false;
        }
    }
</script>

And the markup:

<asp:Button ID="_btnSearch" runat="server" OnClientClick = "return CheckForEmptySearchBox(this.id)" />

This code is working, i.e. when the texbox is empty, the message prompt the user to enter search criteria and the javascript prevents the page to postback. However, when the user enters text, there's no message prompt but the page still doesn't postback. What's wrong?

EDIT

if (boxContent == "") {
     alert("Please enter search criteria");
     return false;
   }
   else {
     return true;
   }

The page is still not posting back.

Community
  • 1
  • 1
Richard77
  • 20,343
  • 46
  • 150
  • 252

3 Answers3

2

you need to return true from your function if you mean it to return true....

AndreasKnudsen
  • 3,453
  • 5
  • 28
  • 33
1
<script type = "text/javascript">
        function CheckForEmptySearchBox(id) {
            var index = id.substring(id.length - 1);
            var boxContent = document.getElementById("contentMain__lvTSEntry__txtClientName_" + index).value;
            if (boxContent == "") {
                alert("Please enter search criteria");
                return false;
            }
else{ return true;}
        } 

you are asking for a return onclientclick function and not returning any value when textbox having value that`s why its stuck

  <asp:Button ID="_btnSearch" runat="server" OnClientClick = "return CheckForEmptySearchBox(this.id)" />
1

You are forgetting to return true if the validation passes:

function CheckForEmptySearchBox(id) {
        var index = id.substring(id.length - 1);
        var boxContent = document.getElementById("contentMain__lvTSEntry__txtClientName_" + index).value;
        if (boxContent == "") {
            alert("Please enter search criteria");
            return false;
        }

     return true; //<--you forgot this
    } 
Icarus
  • 63,293
  • 14
  • 100
  • 115