You must Sign In to post a response.
  • Category: JavaScript

    How to stop submission of an ASP.Net page, using JavaScript?

    Hello,

    I have a ASP.Net button as follow:-
    <asp:Button ID="btn_createAccount" runat="server" Text="Crate my Account" onclientclick="testing()" />


    Now, there are two DropDowns, one is for Year and another is for Month.

    What I am wanting, whenever someone will click this button, then this button will call a javascript function which will check if any value is selected from the two dropdowns.

    The javascript functions is as follow:-
    <script language="javascript" type="text/javascript">
    function testing() {
    if (document.getElementById("ctl00_ContentPlaceHolder1_ddl_year").value == "Select" && document.getElementById("ctl00_ContentPlaceHolder1_ddl_month").value == "Select"){
    document.getElementById("ctl00_ContentPlaceHolder1_Label12").innerHTML = "Please select Year and/or Month";
    document.getElementById("ctl00_ContentPlaceHolder1_ddl_year").focus;
    return false;

    }
    }
    </script>


    The problem is, the "return false" in the javascript function, is not working at all. So, if someone has not selected any value from the year as well as month dropdowns, then it should stop the current page from being posted but unfortunately it is not being happened.

    Please let me know how to resolve this error.
  • #648486
    Just modify your onClientClick event of the button control to add a return statement.

    Please check the following code:


    <asp:Button ID="btn_createAccount" runat="server" Text="Crate my Account" onclientclick="return testing()" />
    [/code[


    Thanks & Regards
    Paritosh Mohapatra
    Microsoft MVP (ASP.Net/IIS)
    DotNetSpider MVM

  • #648537
    Change the below line:

    <asp:Button ID="btn_createAccount" runat="server" Text="Crate my Account" onclientclick="testing()" />


    With this line:

    <asp:Button ID="btn_createAccount" runat="server" Text="Crate my Account" onclientclick="return testing()" />

    Regards,
    Sugandha
    Microsoft Certified Technology Specialist
    MY Blog..

  • #648554
    if it's a serverside control then you can stop it by setting the return to false

    You can set the attributes for the server side controls in the page load event

    Button1.Attributes.Add("onclick", "return blockit()");

    in the blockit function in javascript just set the return as false


    function blockit()
    {
    return false;
    }


    This would not cause the server-side code to run.

    Please mark this as Answer, if this helps

    Regards,
    Alwyn Duraisingh.M 
    << Database Administrator >>
    Jesus saves! The rest of us better make backups...

  • #648613
    On button click event

    button.attributes.add("onclick"," return frmValidate()")


    Javascript Function:



    function frmValidate()
    {
    if (Invd) //the page will not submit
    {
    return false;
    }

    return true; //the page will submit
    }

    Regards
    Mr.Rajesh
    Software Engg.


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.