passing an argument in javascript function
i have a button with id Button1 on page load function i m trying to call javascript function like this
int l = files.Length;
Button1.Attributes.Add("onclick", " alertMe(l);");
where files.length is some integer value,now i m trying to pass this value in alertMe function can anyone tell me is it a write way to pass the value if yes how can i retrieve it in ale开发者_StackOverflowrtMe function
int l = files.Length;
Button1.Attributes.Add("onclick", " alertMe(" + l + ");");
function alertMe(length)
{
alert("you passed a length of: " + length);
}
In your sample, the value passed to the javascript function is always 1. Also, you might want to use the Button.OnClientClick
property instead, as this ensures that ASP.NET's own button handling code is left intact. Your C# code should probably look something like this:
int fileCount = files.Length;
Button1.OnClientClick = "alertMe(" + fileCount + ");"
In the javascript, make sure you declare the formal parameter in the function signature:
function alertMe(fileCount)
{
alert(fileCount);
}
try:
int l = files.Length;
Button1.Attributes.Add("onclick", " alertMe(" + l.ToString() + ");");
精彩评论