writing parameter dynamically

  • Thread starter Thread starter Adam Right
  • Start date Start date
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
 
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.
 
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...
 
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
 
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.
 
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
 
Back
Top