Click here to Skip to main content
15,880,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I write function on button click event but alert didn't fire.
btnSubmit.Attributes.Add("onclick", "return check();");--code Behind
function check() --Java Script on cleint side
{
alert("The KRA can't empty");
}
And Confirm fires on 2nd click.

btnSubmit.Attributes["onClick"] = "javascript:return check();";--code Behind
function check() --Java Script on cleint side
{
var answer = confirm("Are you sure to save ?")
if (answer)
{
return true;
}
else
return false;
}
Kindly suggest,please.
Posted
Comments
Toniyo Jackson 25-Nov-10 5:24am    
Where r u keeping ur javascript? In ur page itself or separate javascript file?

Try this,
btnSubmit.Attributes.Add("onclick", "javascript:return check()")
 
Share this answer
 
v2
Comments
Nilesh from Mumbai 25-Nov-10 5:07am    
Hi Toni ,Thanks.
But the function call on second click not on first click.
Toniyo Jackson 25-Nov-10 5:24am    
Where r u keeping ur javascript? In ur page itself or separate javascript file?
Nilesh from Mumbai 25-Nov-10 6:03am    
On the upper side of our page Itself.
Toniyo Jackson 25-Nov-10 6:07am    
Where did u coded this "btnSubmit.Attributes.Add("onclick", "return check();");"?
Nilesh from Mumbai 25-Nov-10 6:29am    
protected void btnSubmit_Click(object sender, EventArgs e)
{
double dblTotal = 0;
dblTotal = dblTotal + dblEarn;

if (dblTotal != 100)
btnSubmit.Attributes.Add("onclick", "javascript:return checkEmpty()");
else
btnSubmit.Attributes.Add("onclick", "javascript:return checkOk()");

//If CheckOK then send to another page else on same page
Response.Redirect("~/SC/frmSCard.aspx");
}
Try as Toni says. Also return true or false from function check based on your requirement.
 
Share this answer
 
Comments
Nilesh from Mumbai 25-Nov-10 5:05am    
Could tell please,
How to check return true or false on code behind?
e.g.if (txtCkeckVal.Text == "")
{
btnSubmit.Attributes.Add("onclick", "javascript:return checkEmpty()");
}
else
btnSubmit.Attributes.Add("onclick", "javascript:return checkOK()");


Thanks Nilesh.
Vicky.tts 15-Mar-12 4:33am    
dcfffffff
Brij 25-Nov-10 5:14am    
I think, you got something wrong. Here your functions checkEmpty and checkOK should return true or false. If it returns true , then there will be postback after execution of the JS method and if it returns false, then there will be no postback after it
Add the below code in page load.
C#
protected void Page_Load(object sender, EventArgs e)
{
    btnSubmit.Attributes.Add("onclick","javascript:return check()");
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900