简体   繁体   中英

JavaScript Function on Button Click

Hi I am working on java script,i am writing java script function on aspx page and calling that function,the function will return value on button click event,But when i click on button the function is not getting called Here is my code.

Java Script Function

function CheckAge(age)
{
if (age > 18)
return true;
else
return false;                
}

Here is on Button Click Event in CSfile

try
{
bool check = false;
try
{
btnAge.Attributes.Add("onClick", "javascript:return CheckAge('" + txtAge.Text + "')");
if (check)
Response.Write("You Are Eligible");
else
Response.Write("You Are Not Eligible");
}
catch (Exception err)
{
Response.Write(err.Message);
}
}
catch (Exception)
{
throw;
} 
}

Please help me out,can give the code please

the variable check is not set. try this:

  btnAge.Attributes.Add("onClick", "javascript:var check=CheckAge('" + txtAge.Text + "')");

The problem is the .aspx code is computed before the page is sent to the user. The code will all be ran at once before the user ever receives the page. Once the user has the page, no amount of javascript could run aspx unless it refreshes the page or calls another one.

Either make the entire function in Javascript, or make it reload the page with a GET variable that the aspx can process.

JS:

function CheckAge(age)
{
    if (age > 18) document.location.href = "index.aspx?allowed=1";
    else document.location.href = "index.aspx?allowed=0";                
}

ASP:

try
{
    if(Request.QueryString["allowed"]==1) Response.Write("You Are Eligible");
    if(Request.QueryString["allowed"]==0) Response.Write("You Are Not Eligible");
}
catch (Exception)
{
    throw;
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM