0

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 alertMe function

4 Answers 4

2
int l = files.Length;
Button1.Attributes.Add("onclick", " alertMe(" + l + ");");
Sign up to request clarification or add additional context in comments.

4 Comments

thnx but how can i retrieve it in the alertMe function
Fabian Vilers' response will help you with that part
Still, I think it would be better to use the Button.OnClientClick property. See my answer for an example.
That's definitely a valid point, i upvoted you for that. I just wanted to show with the least amount of modification, what sumit was doing wrong.
1
function alertMe(length)
{
    alert("you passed a length of: " + length);
}

Comments

1

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);
}

Comments

0

try:

int l = files.Length;
Button1.Attributes.Add("onclick", " alertMe(" + l.ToString() + ");");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.