writing parameter dynamically

A

Adam Right

Hi All

How can i write dynamic parameter for a java script function in<asp:image>
tag?

for static usage like ;
onmouseover="ChangePicture('images/blue.gif')"
works fine.

I have to use single quotes because the parameter must be string. If i try
like that;

onmouseover='<%# "ChangePicture('" + Eval("BIG_PICTURE_PATH") + "')" %>'

it raises "The server tag is not well formed" error ....

What should I do?

Thanks,

Adam
 
N

Nathan Sokalski

Try something like the following:

'<%#
DataBinder.Eval(Container.DataItem,"BIG_PICTURE_PATH","ChangePicture('{0}');")
%>'

This databinding expression uses a format string as the third parameter. I
usually find it easier to use simple format strings like this when I want to
include other text along with the value being databound (there are less
quotes that get nested inside of each other, as well as making the need for
concatenation much less common). For more details on databinding expressions
and format strings, see the documentation.
 
A

Adam Right

Hi Nathan,

It does not work, still same problem..
the single quotes in '{0}' causes the error "The server tag is not well
formed" error ,
if i remove the single quotes no error occurred but function gets error...
 
S

S. Justin Gengo

Adam,

You should be able to use double quotes instead of single quotes around the
{0} but you'll have to double up the double quotes like this: ""{0}""

Regards,

S. Justin Gengo

Free code and component libraries at:
http://www.aboutfortunate.com
 
N

Nathan Sokalski

What is the complete code for the tag in the *.aspx file? When I use things
the way I showed you, even with single quotes, they work perfectly. What is
the complete exact text of the error message? I am wondering if it is
possible that the error is referring to a different server tag.
 
J

Jose A. Fernandez

Hello Adam

With a htmlcontrol img (tag <img .. />
<asp:FormView ID="FormView1" runat="server"...
<ItemTemplate>...
<img src="/images/image1.jpg" onmouseover="<%#
String.Concat("ChangePicture('", Eval("BIG_PICTURE_PATH"), "');") %>" /
the render is
<img src="/images/image1.jpg" onmouseover="ChangePicture('/
images/OtherImage.jpg');" />


But, if you have webcontrol Image...
<asp:FormView ID="FormView1" runat="server"...
<ItemTemplate>...
<asp:Image runat="server" ID="imgExample" ImageUrl="~/
images/image1.jpg" />

and into codebehind, in ItemCreated method
Protected Sub FormView1_ItemCreated(ByVal sender As Object, ByVal
e As System.EventArgs) Handles FormView1.ItemCreated

Dim pathImagen As String = CType(FormView1.DataItem,
Data.DataRowView).Item("CategoryID").ToString
Dim imgExample As Image =
CType(FormView1.FindControl("imgExample"), Image)

imgExample.Attributes.Add("onmouseover", "ChangePicture('" &
pathImagen & "');")

End Sub

the render id
<img id="ctl00_contenidoCentral_FormView1_imgExample"
onmouseover="ChangePicture('/images/OtherImage.jpg');" src="../images/
image1.jpg" ...


Links
--------------
WebControl.Attributes Property
Gets the collection of arbitrary attributes (for rendering only) that
do not correspond to properties on the control.
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.attributes(VS.80).aspx

______________________
Jose A. Fernandez
blog: http://geeks.ms/blogs/fernandezja
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top