views:

1648

answers:

3

I'm not able to get this working and I can't figure out why.

<ItemTemplate>
<% if (Field(((DataRowView)(Container.DataItem)), "Video File")  != "") {  %> 
<a href='upload/images/<%# Field(((DataRowView)(Container.DataItem)), "Video File")%>'>Download Link</a>
<% } else {  %>
<embed height="14" width="661" name="plugin" src="<%# ContentUploadURL%>/<%# Field(((DataRowView)(Container.DataItem)), "Audio File")%>" type="audio/mpeg" autostart="false" />
<% } %>
</ItemTemplate>

It seems simple enough, but I just get this error:

Compiler Error Message: CS0103: The name 'Container' does not exist in the current context

I've been at this all day and I'm a total newbie working on a CMS in asp. I don't really want to learn ASP, just to get this one thing working.

If anyone could point me in the right direction, I'd be very very grateful.

Thanks!

+4  A: 

You cannot use Container.DataItem outside databinding expressions <%# ... %>.

I suggest you change your code to something like this (sorry but I can't test it currently):

<ItemTemplate>
  <asp:HyperLink runat="server"
    Visible='<%# Eval("Video File") != "" %>'
    NavigateUrl='<%# Eval("Video File")' Text="Download Link" />

  <embed runat="server" Visible='<%# Eval("Video File") == "" %>'
    height="14" width="661" name="plugin"
    src="<%# ContentUploadURL%>/<%# Field(((DataRowView)(Container.DataItem)), "Audio File")%>"
    type="audio/mpeg" autostart="false" />
</ItemTemplate>

The key is to set the Visible property of the two controls based on the "Video File" field of the data item.

See also this question: http://stackoverflow.com/questions/653486/asp-net-conditional-databinding

M4N
A: 

Oh, thank you! That definitely helped. Ok, so I actually need some more html inside, so I tried the placeholders technique you pointed me to.

So, I have this now:

<asp:PlaceHolder id="PlaceHolder1" runat="server" Visible='<%# Eval("Video File") != "" %>'>
Video Stuff
</asp:PlaceHolder>

<asp:PlaceHolder id="PlaceHolder2" runat="server" Visible='<%# Eval("Video File") == "" %>'>
Audio Stuff
</asp:PlaceHolder>

It almost works except that Video File is visible on both instances when it should only be for the first one.

I also tried this:

<asp:PlaceHolder id="PlaceHolder1" runat="server" Visible='<%# Eval("Video File") != "" %>'>
    Video Stuff
    </asp:PlaceHolder>

<asp:PlaceHolder id="PlaceHolder2" runat="server" Visible='<%# Eval("Audio File") != "" %>'>
    Audio Stuff
    </asp:PlaceHolder>

But that made both show all the time. I feel like it's almost there.

Thanks!

A: 

Oh, nevermind, I got it!!

This worked finally:

<asp:PlaceHolder id="PlaceHolder1" runat="server" Visible='<%# Field(((DataRowView)(Container.DataItem)), "Video File") != "" %>'>
Video Stuff
</asp:PlaceHolder>

<asp:PlaceHolder id="PlaceHolder2" runat="server" Visible='<%# Field(((DataRowView)(Container.DataItem)), "Video File") == "" %>'>
Audio Stuff
</asp:PlaceHolder>

Thank you SO much!

Glad you got it. Next time, edit your original question instead of creating an answer. That's how it's done here. Also, FYI, you can avoid the ugly long code blocks by making sure the lines aren't too long. Wrap them by hand.
John Saunders