1

I am trying to call a javascript function from asp.net codebehind using onclientclick for a linkbutton. Code is like this:

string x = "redirecttoimagepage"+"("+ y+")";//y has an int value i need and it varies
lnkimagepage.OnClientClick = x;

i need to use the value y in the javascript function for using.

 function redirecttoimagepage(variable)
{
var strURL = "Subpages.aspx?tabID="+variable;
window.open(strURL,"_blank");
return true;
}

Right now, with the code i have shared, it doesnt do anything. After the link is clicked, it stays in the same page. What am i doing wrong? Appreciate all your help guys. Thanks a lot!

4 Answers 4

4
string x = "redirecttoimagepage"+"('"+ y+"')";
lnkimagepage.Attributes.Add("onclick",x);

EDITED

in addition, just noticed that lnkimagepage is a LinkButton so it will be hard to invoke redirecttoimagepage script as LinkButtons are already associated with __doPostBack jscript. use HyperLink instead of LinkButton

Sign up to request clarification or add additional context in comments.

5 Comments

How would this be different from OnClientClick?
@Domenic asp.net 2.0 (and above) have a OnClientClick property, This is basically a shortcut for Attributes[”onclick”]. no differences... its just a way I got used to write a code...
OK, then my question is why is this different from the OP's code. I guess the stuff after the edit is relevant, but otherwise it does the exact same thing.
remo did missed the apostrophe, as he had "redirecttoimagepage"+"("+ y+")". the right form is "redirecttoimagepage"+"('"+ y+"')". test urselft the first version wil give u x equals to redirecttoimagepage(value_of_y) while my version gives redirecttoimagepage('value_of_y'). this way jscript redirecttoimagepage is called and value_of_y is passed as parameter
Ah! Damn, hard to spot those little apostrophes :P. OK, +1 it is.
1

I am trying to call a javascript function from asp.net codebehind using onclientclick for a linkbutton. Code is like this:

Your doing it wrong. The HTTP model is a model of requests. The page talks to the server not the other way around.

If you truly need a bi-directional communication channel use WebSockets

1 Comment

It seems like he's doing it (conceptually right) but his explaination is ridiculous. It seems like he does not know what "to call a function" means.
0
string x = "redirecttoimagepage"+"('"+ y+"')"; 
lnkimagepage.Attributes.Add("onclick",x);

**in javascript**
window.open("PageName.aspx");

Comments

-1

I think you must specify complete url with window.open not just page name.....

1 Comment

This is false; it works just like any other URL and can be either relative or absolute.

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.