Forums » .NET » ASP.NET »

how to call javascript on button click


Posted Date: 23 Sep 2008      Posted By:: G.Hemadribabu     Member Level: Gold    Member Rank: 452     Points: 1   Responses: 11



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




Responses

#298937    Author: Pradeep Kintali      Member Level: Gold      Member Rank: 348     Date: 23/Sep/2008   Rating: 2 out of 52 out of 5     Points: 3

//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    Author: K. B. Rao      Member Level: Silver      Member Rank: 0     Date: 23/Sep/2008   Rating: 2 out of 52 out of 5     Points: 1

Hi,


Below is the sample code.


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


 
#298939    Author: Sam      Member Level: Bronze      Member Rank: 0     Date: 23/Sep/2008   Rating: 2 out of 52 out of 5     Points: 4

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    Author: D.Jeya kumar(JK)      Member Level: Gold      Member Rank: 13     Date: 23/Sep/2008   Rating: 2 out of 52 out of 5     Points: 4

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    Author: paresh      Member Level: Silver      Member Rank: 0     Date: 23/Sep/2008   Rating: 2 out of 52 out of 5     Points: 4

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    Author: bill      Member Level: Gold      Member Rank: 952     Date: 23/Sep/2008   Rating: 2 out of 52 out of 5     Points: 5

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    Author: Victoria      Member Level: Gold      Member Rank: 355     Date: 23/Sep/2008   Rating: 2 out of 52 out of 5     Points: 2

in the Page_Load you have to add the attribute..

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


 
#298968    Author: vamshi krishna      Member Level: Silver      Member Rank: 0     Date: 23/Sep/2008   Rating: 2 out of 52 out of 5     Points: 4

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    Author: G.Hemadribabu      Member Level: Gold      Member Rank: 452     Date: 23/Sep/2008   Rating: 2 out of 52 out of 5     Points: 3

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    Author: Vivek Sharma      Member Level: Gold      Member Rank: 568     Date: 23/Sep/2008   Rating: 2 out of 52 out of 5     Points: 4

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    Author: Ravi Kiran Nedunuri      Member Level: Gold      Member Rank: 625     Date: 24/Sep/2008   Rating: 2 out of 52 out of 5     Points: 4

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


 
Post Reply

 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.



Next : webservice
Previous : Datagrid view in asp.net
Return to Discussion Forum
Post New Message
Category: ASP.NET

Related Messages

Subscribe to Subscribers
Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Talk to Webmaster Tony John
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India