C# 在 ItemTemplate 中使用 Container.DataItem
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/758418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use of Container.DataItem in an ItemTemplate
提问by
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
编译器错误消息:CS0103:当前上下文中不存在名称“容器”
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.
我一整天都在做这个,我是一个在 asp 中使用 CMS 的新手。我真的不想学习 ASP,只是为了让这件事起作用。
If anyone could point me in the right direction, I'd be very very grateful.
如果有人能指出我正确的方向,我将非常感激。
Thanks!
谢谢!
回答by M4N
You cannot use Container.DataItem outside databinding expressions <%# ... %>.
您不能在数据绑定表达式 <%# ... %> 之外使用 Container.DataItem。
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.
关键是根据数据项的“Video File”字段设置两个控件的Visible属性。
See also this question: ASP.Net conditional databinding
另见这个问题:ASP.Net 条件数据绑定
回答by M4N
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.
哦谢谢!那肯定有帮助。好的,所以我实际上需要更多的 html,所以我尝试了你指给我的占位符技术。
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!
谢谢!
回答by M4N
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 SOmuch!
谢谢SO了!