如果语句在转发器中使用ItemTemplate
我正在使用ASP.NET Repeater
来显示
Some data |
它工作正常,但我想在ItemTemplate
有一个if()
语句,所以我可以有条件地确定我是否要打印出一个
所以我想要这样的东西:
Some data
有什么办法可以实现这个目标吗?
PS。 CurrentItemCount
刚刚组成。 我还需要一种方法来获取if()
语句中的当前项目计数。 但我似乎只能从获得它
,不能与
if()
语句一起使用?
我会使用codebehind:
protected void OnCheckboxListItemBound(Object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { HtmlTableRow itemRow = (HtmlTableRow) e.Item.FindControl("itemRow"); itemRow.Visible = e.Item.ItemIndex % 2 == 0; } }
另一种方法(如果性能不是问题):
如果你正在尝试制作一个2列表,这可以解决问题
<%# Container.ItemIndex % 2 == 0 ? "" : "" %> Some data <%# Container.ItemIndex % 2 != 0 ? " : "" %>
改变了一些事情:所有行的id="itemRow"
会导致重复的id不允许。
删除了runat="server"
因为在这种情况下没有意义。
我有两个示例,对于示例,我将转发器绑定到一个字符串数组(仅用于演示目的)
void BindCheckboxList() { checkboxList.DataSource = new string[] { "RowA", "RowB", "RowC", "RowD", "RowE", "RowF", "RowG" }; checkboxList.DataBind(); }
示例1:在de codebehind中创建一个方法,然后将绑定元素投射回来评估您想要的值。
在CodeBehind中创建Methode(示例1):
protected string StringDataEndsWith(object dataElement, string endsWith, string returnValue) { // for now an object of the type string, can be anything. string elem = dataElement as string; if (elem.EndsWith(endsWith)) { return returnValue; } else { return ""; } }
在.aspx文件中(示例1):
<%# StringDataEndsWith(Container.DataItem,"A","") %> <%# Container.DataItem %> <%# StringDataEndsWith(Container.DataItem,"G"," ") %>
示例2:您可以在.aspx文件中使用直接强制转换
DirectCast示例(后面没有代码):
<%# Convert.ToString(Container.DataItem).EndsWith("A") ? "" : "" %> <%# Container.DataItem %> <%# Convert.ToString(Container.DataItem).EndsWith("G") ? " " : "" %>
我希望这就是你要找的东西。 问候。
如果您想要对其他项目执行某些操作,请使用交替项目模板。 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.alternatingitemtemplate.aspx
- 无法在ASP.NET 5 Core中使用iTextSharp
- EF 4 – 没有代理的延迟加载
- 在c#asp.net中动态分配按钮事件
- 在静态类中访问HttpContext.Current
- Doc文件不从asp.net中的UserControl下载
- C#HttpRuntime.Cache.Insert()不保存缓存值
- 使用XUnit和ASP.NET Core 1.0进行dependency injection
- 自定义validation属性MVC2
- 将角色添加到’CreateUserWizard’
Interesting Posts