Question

I have a single page asp.net 4.0 form with couple of textboxes in divs which I programmatically show/hide. On one particular textbox on my form with onkeypressevent defined like this

<fieldset>
    <div class="WizardStepDiv">
        <label for="MobileNumber">
            Please enter your mobile number</label>
        <%--<asp:TextBox name="vmobilenumber" ID="vmobilenumber" runat="server" Style="width: 150px"></asp:TextBox>--%>
        <input type="text" name="vmobilenumber" id="vmobilenumber" runat="server" style="width: 150px"
            onkeypress="javascript: return searchKeyPress(event);" />
    </div>
    <div class="navigationArrows">
        <div style="margin-left: 35%; width: 165px;">
            <div class="previousbutton" style="float: left; padding: 3px 0 0 6px">
                <a href="#" onclick="NavigatetoPreviousScreen()">
                    <img src="image/left_arrow.png" width="18" height="25" alt="navigateprevious" style="background-image: url(image/arrow_box.png)" />
                </a>
            </div>
            <div class="nextbutton" style="float: right; padding: 3px 0 0 6px">
                <a href="#" id="searchMobileNumber" onclick="NavigatetoNextScreen()">
                    <img src="image/right_arrow.png" width="18" height="25" alt="navigatenext" style="background-image: url(image/arrow_box.png)" />
                </a>
            </div>
        </div>
    </div>
</fieldset>

The searchKeyPress(event) is defined like this

function searchKeyPress(e) {
// look searchcheckoutKeyPress for window.event in case event isn't passed in
if (typeof e == 'undefined' && window.event) { e = window.event; }
if (e.keyCode == 13) {
    document.getElementById('searchMobileNumber').click();
    if (e.stopPropagation) {
        e.stopPropagation();
    } else {
        e.cancelBubble = true;
    }
  (event.preventDefault) ? event.preventDefault() : event.returnValue = false;
}

}

The NavigatetoNextScreen() function validates the mobilenumber textbox for not empty and if it passes validation it makes an ajax call like this (portions cut out below)

    var mobilenumber = $.trim($("#vmobilenumber").val());
        if (mobilenumber == "" || !isNumber(mobilenumber)) {
            error += "<span>Please enter a valid mobile number to proceed to the next step</span></br>";

        }
        if (error == "") {
            $("#errormessage").hide();
            methodName = "GetVistorByMobileNumber";
            $.support.cors = true;
            $.ajax({
                type: 'GET',);

When there is validation error the page does not postback, however on the ajax call returning success or error (after all processing in both cases) the webpage postsback which I do not want.

This behavior is only on Firefox, Chrome and IE do not postback. What could be the issue?

Thanks in advance for your help

Was it helpful?

Solution

<a href="#" id="searchMobileNumber" onclick="NavigatetoNextScreen()">

Pass event object in this function YouNavigatetoNextScreen(evt) and call evt.preventDefault();

function YouNavigatetoNextScreen(){
   //... your code
   evt.preventDefault();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top