Yes there's a syntax error. You have a ' instead of a " and your missing a + sign...
Please be aware that you cannot access Session variables from JavaScript.
Therefore what you should be doing is creating a String containing the JavaScript that should be executed when the image is clicked.
The JavaScript that needs to be executed is:
-
window.open('http://abc.com/Home.aspx?uid=12345');
-
This will open the Home.aspx page in the abc website in a new window...passing it uid=12345.
The thing is that 12345 is stored in Session on the server. That means that when you create the string containing the JavaScript you need to add the value that is stored in session:
-
Public ReadOnly Property ImageOnclickJavaScriptString() As String
-
Get
-
Dim javaScriptString as String
-
javaScriptString ="window.open('http://abc.com/Home.aspx?uid="
-
javaScriptString = javaScriptString + Session("uid")
-
javaScriptString = javaScriptString + "');"
-
return javaScriptString
-
End Get
-
End Property
-
Note that I put this into a Public Property. I could have put it into a Protected Property...but the important thing is that this property is accessible to the ASP code.
Now in your ASP code you can call the Response.Write() method to write this string into the HTML <img ...> tag. The ASP short and for calling the Response.Write method is: <%= %>
So, in your ASP code you'll have:
-
<img alt="" onclick="<%= Me.ImageOnclickJavaScriptString %>"; src="../myimg.jpg" onmouseover="this.style.cursor='pointer';" />
(All this time I've been assuming you're using VB.NET...if you're using C# you need to modify everything to use that syntax instead)
To make things a lot easier for yourself it would be a good idea to use an ASP.NET Image control instead of an HTML <img>. Image controls are accessible on the server and it makes things a little easier...
If you change your <img> tag you'd have:
- <asp:Image id="myImg" runat="server" ImageURL="~/myimg.jpg" />
Now in your server code you could just simply set the onclick event of the Image like so:
-
Dim javaScriptString as String
-
javaScriptString ="window.open('http://abc.com/Home.aspx?uid="
-
javaScriptString = javaScriptString + Session("uid")
-
javaScriptString = javaScriptString + "');"
-
-
myImg.Attributes.Add("onclick",javaScriptString)
-
myImg.Attributes.Add("onmouseover","this.style.cursor='pointer';")
-
You'd place this in your Page Load event when IsPostback = False...or in your Page PreRender event if you want....anywhere really, so long as it's done the first time the page is loaded. You only have to do it the first time the page loads because the ASP.NET Image control remembers this information between postbacks (whereas the HTML <img> tag does not).
-Frinny