GridView and TemplateField

Hey Experts,

I am trying to have an ImageButton as a Template Field in a GridView control.
I simply would like to display the image and have an OnClientClick event attached to a window.open(); function.
My syntax is flawed and cannot find a way around it. I will always render the <%%> tags as encoded html.

Thanks all,
Goosey

<Columns>
     <asp:TemplateField>
          <ControlStyle BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" Height="280px" Width="300px" />
      <ItemTemplate>
      

<asp:ImageButton ID="Image1" runat="server" ImageUrl='<%# Eval("Item") %>' OnClientClick='javascript:window.open("<%# Eval("Item") %>");' />                                          


</ItemTemplate>
     </asp:TemplateField>
</Columns>
LVL 1
Goosey1313Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jason ScolaroCommented:
<asp:ImageButton ID="Image1" runat="server" ImageUrl='<%# Eval("Item") %>' OnClientClick='<%# "'javascript:window.open(" & Eval("Item") & ");" %>' />  
craskinCommented:
'javascript:window.open("<%# Eval("Item") %>");'

looks like you're nesting double quotes which i don't think can work. try using single quotes around Item. i don't really know what you're trying to evaluate, but can it be accomplished with Container.DataItem instead?
craskinCommented:
ah nevermind, scolja's suggestion is better.
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Goosey1313Author Commented:
Jason to the rescue as always :)

Unfortunetly, I get this

Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: The server tag is not well formed
Jason ScolaroCommented:
Goosey1313,

Sorry I let a single quote sneak by me in the beginning (it's hiding in front of the javascript:window.open).

<asp:ImageButton ID="Image1" runat="server" ImageUrl='<%# Eval("Item") %>' OnClientClick='<%# "javascript:window.open(" & Eval("Item") & ");" %>' />  

-- Jason
craskinCommented:
try changing '<%# "'javascript:window.open(" & Eval("Item") & ");" %>'

to "'javascript:window.open(" & <%# Eval("Item") %> & ");' "
Goosey1313Author Commented:
Nope same thing :(
Goosey1313Author Commented:
This is for scolja last comment.

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0019: Operator '&' cannot be applied to operands of type 'string' and 'object'
Jason ScolaroCommented:
Goosey1313,

Hmm, I tested it before sending it to you... what is inside of the column Item?  Are there quotes?

-- Jason
Jason ScolaroCommented:
Goosey1313,

Are you using VB.NET or C#?  If C#, then change & to +

-- Jason
craskinCommented:
it sounds like the eval("item") is not returning a string. you'll have to cast it as one.
Goosey1313Author Commented:
here is the code


<Columns>
      <asp:TemplateField>
            <ControlStyle BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" Height="280px" Width="300px" />
            <ItemTemplate>
                  <asp:ImageButton ID="Image1" runat="server" ImageUrl='<%# Eval("Item") %>' OnClientClick='<%# "javascript:window.open(" & Eval("Item") & ");" %>' />  
            </ItemTemplate>
      </asp:TemplateField>
</Columns>
Jason ScolaroCommented:
Goosey1313,

That's strange, I just copied your same code and it works for me.... I'm thinking the problem is with what's inside of the Eval("Item").  What's the current error you are receiving?

-- Jason
Goosey1313Author Commented:
Ok getting closer,

It renders in the browser like this.

onclick="javascript:window.open(media/Interior01.jpg);"

i need it to be like this

onclick="javascript:window.open('media/Interior01.jpg');"
Jason ScolaroCommented:
Goosey1313,

Simple enough:
<asp:ImageButton ID="Image1" runat="server" ImageUrl='<%# Eval("Item") %>' OnClientClick='<%# "javascript:window.open(""" & Eval("Item") & """);" %>' />  

-- Jason
Goosey1313Author Commented:
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1026: ) expected
Jason ScolaroCommented:
Goosey1313,

I've tested this over and over now... what you have should work fine.. here's what I've got:

<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='<%# Eval("Item") %>' OnClientClick='<%# "javascript:window.open(""" & Eval("Item") & """);" %>' />

Renders as:

<input type="image" name="ctl00$contentHolder$grdView$ctl03$ImageButton1" id="ctl00_contentHolder_grdView_ctl03_ImageButton1" src="/mmc.gif" onclick="javascript:window.open(&quot;/mmc.gif&quot;);" style="border-width:0px;" />

-- Jason
Goosey1313Author Commented:
Sorry you have had to baby step me through this. I really do thank you for the time you heave spent on this as well.
I definitly owe you more then 25 points for this, which I will make sure happens.

But, I copy and paste your code directly into mine and I get this error

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1026: ) expected

I am using C# and have tried changeing the & to the + and gives the same error.
Jason ScolaroCommented:
Goosey1313,

OK, C# is going to be the problem.. I don't think two double quotes translates in C#.  Try this line:
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='<%# Eval("Item") %>' OnClientClick='<%# "javascript:window.open(\"" + Eval("Item") + "\");" %>' />

I'm not able to test that, but hopefully that'll do the trick.

-- Jason

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Goosey1313Author Commented:
Dude, you are an ASP.NET GOD!

And a really cool guy for sticking it out for over an hour with me, just to make a link work on an image, lol.

When I get some points back there all yours!

Thanks again.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.