Link to home
Start Free TrialLog in
Avatar of Goosey1313
Goosey1313

asked on

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>
Avatar of Jason Scolaro
Jason Scolaro
Flag of United States of America image

<asp:ImageButton ID="Image1" runat="server" ImageUrl='<%# Eval("Item") %>' OnClientClick='<%# "'javascript:window.open(" & Eval("Item") & ");" %>' />  
'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?
ah nevermind, scolja's suggestion is better.
Avatar of Goosey1313
Goosey1313

ASKER

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
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
try changing '<%# "'javascript:window.open(" & Eval("Item") & ");" %>'

to "'javascript:window.open(" & <%# Eval("Item") %> & ");' "
Nope same thing :(
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'
Goosey1313,

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

-- Jason
Goosey1313,

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

-- Jason
it sounds like the eval("item") is not returning a string. you'll have to cast it as one.
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>
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
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');"
Goosey1313,

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

-- Jason
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
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
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.
ASKER CERTIFIED SOLUTION
Avatar of Jason Scolaro
Jason Scolaro
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.