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

    how to call javascript on button click

    hi,

    i had written the javascript for checking a textbox , value whihc entered is numbers alone, if it s char thenalrt msg

    now how to callthe same on the button click event

    function valNumeric()
    {
    if (event.keyCode >= 48 && event.keyCode <= 57) event.returnValue = true;
    elseevent.returnValue=false;
    }

    how to call this script on button click , pls tel me
  • #298937
    //Add the below code in

    PageIntialisation

    buttonName.Attributes.Add("onclick", "javascript: valNumeric();");

    Or else

    <asp:Button id="buttonid" runat="server" OnClientClick="valnumeric()"></asp:Button>

  • #298938
    Hi,


    Below is the sample code.


    <INPUT TYPE="submit" onclick="valNumeric()"/>

  • #298939
    hi,

    There is a property available in ASP.NET button control called 'onclientclick'. you can use this property to call javascripts.

    Example:
    asp:button id="Button1"
    usesubmitbehavior="true"
    text="Open Web site"
    onclientclick="Navigate()"
    runat="server" onclick="Button1_Click" />

  • #298941
    Hi,


    You can call the java script from two places. directly from a button click of from server side page load or button click


    by using the below client side button you can call

    <input id="Button2" type="button" onclick="fun();" value="button" />

    otherwise

    Page.RegisterClientScriptBlock("test", "<script language='javascript'>fun();</script>");

    Regards
    JK

    Thanks & Regards
    D.Jeya Kumar(JK)

  • #298950
    hi

    IF you are calling javascript function in codebehind than

    btn_submit.Attributes.Add("onClick", "javascriptfunctionname();");

    IF you are calling javascript function in aspx page(html)

    <asp:Button id="btn_submit" runat="server" OnClientClick="javascriptfunctionname();"></asp:Button>

  • #298958
    hi

    the id of textbox on which you enter the value is TextBox1

    <script language="JavaScript" type="text/javascript">
    function valNumeric()
    {
    if (document.form1.TextBox1
    >= 48 && document.form1.TextBox1 <= 57)
    alert("true");
    else
    alert("false");
    }
    </script>


    you can call the javascript using onclientclick.

    <asp:Button ID="Button1" onclientclick="valNumeric()" runat="server" Text="Button" />

  • #298960
    in the Page_Load you have to add the attribute..

    btn.Attribute.Add("onClick","javascript:valNumeric();");

  • #298968
    Hi,
    in design u write the following code
    <script language="javascript">
    function Validate()
    {
    if(document.Form1.txtname.value=="")
    {
    alert("name should be selected...");
    return false;
    }
    return true;
    </script>

    Write the following code on .cs file in page load event


    Button1.Attributes.Add("OnClick","return Validate()");

  • #298975
    hi friends

    in my above javascript where can i include my textbox id , i am confused, please guide me , and i got that script in the net


    thank you
    hemadribabu

  • #298993
    Hi,

    You can call java script function by calling it on pageload

    //Write this Code on page load//

    btnSave.Attributes.Add("onClick", "return validate();");

    consider btnSave is the id of button and validate is a

    function of java script we have made in aspx page.

    Regards
    Vivek Sharma

  • #299700
    in page load event write
    btnSubmit.Attributes.Add("onclick", "return valNumeric();");


    Or write in .aspx page as

    <asp:Button id="btnSubmit" runat="server" Text="Submit" OnClientClick="javascript:return valNumeric();"></asp:Button>

    the above are the two ways to call a javascript function

    Regards

    N.RaviKiran


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.