虽然Container.ItemIndex
作为一种在数据绑定表达式中获取当前中继器项索引的方法<%# Container.ItemIndex %>
工作得很好,但它在纯代码中不起作用。
我如何在这里获得中继器的当前项目索引:
<ItemTemplate>
<% If Container.ItemIndex = 2 Then %>
TRUE/some longer HTML here/
<% Else %>
false/some longer HTML here/
<% End If %>
</ItemTemplate>
字符串
编辑
对于没有太多的HTML代码的情况下,这将工作,但我正在寻找代码渲染块解决方案,如上面的例子。<%#: If(Container.ItemIndex = 2, "TRUE", "false") %>
个
2条答案
按热度按时间4ioopgfo1#
你不能在模板化控件的模板中使用“代码渲染块”。如果你只关心你的标记块是否会被渲染成HTML,而不管它是否被评估,你可以使用控件的
Visible
/visible
属性。字符串
i7uaboj42#
好的,只需按下一个按钮,然后拿起中继器行。
所以,假设我们有这个repeater标记:
字符串
和我们的代码后面来填充这个中继器:
型
我们现在有了这个:
x1c 0d1x的数据
现在是按钮代码。注意我是如何放入一个标准按钮的。
但是,我们现在不能双击按钮来自动连接并创建事件。
但是你可以这样做来创建一个事件:
在按钮代码标记中,键入OnClick=
当你点击“="时,intel-sense会弹出一个对话框,让你创建一个按钮点击事件,如下所示:
的
所以,现在点击创建新事件。它“似乎”什么也没发生,但如果我们翻到代码背后,你会发现点击事件存根。
所以,现在在我们的点击事件代码中,我们可以很容易地获取当前的重复行,值,甚至是索引。
代码如下:
型
产出:
型
因此,只需放入一个plane jane按钮。当您单击它时,事件存根就会运行,正如您所看到的,我们选择了“Repeater row item”。
从这一行,我们可以得到行索引,当然还可以使用find control从这一行中取出任何其他控件值。
例如:
型
所以,一旦你有了repeater行,你就不需要一些container.ItemIndex表达式了,因为行项允许你通过以下方式获取给定的索引行:
型
因此,ItemIndex是可用的,并且无论您是否在标记中包含或使用conttin.ItemIndex都是可用的。
在标记中几乎不需要对这些代码进行处理。
请注意上面是多么的干净和简单。我建议你做出巨大的努力来避免像你正在做的那样将vb代码倾倒在标记中。如果你甚至想转换为c#,或者只是维护这些代码?把这些代码放在代码后面的区域-而不是标记中。
如上所述,对于重复数据等,您可以使用Repeater,甚至我经常使用listview -因为它允许自动创建重复布局,而无需编写循环代码。
好了,现在我们已经完成了,假设我们想在中继器中输入一条消息,
型
或
型
好吧,在我们的标记中,我们可以添加这样的标签:
现在,在repeater属性表中,双击此处以创建项目数据绑定事件:
的
现在很简单,我们可以像这样为每一行添加代码逻辑:
型
现在的输出是这样的:
的
所以,再一次,要对repeater进行条件格式化,为活动酒店添加颜色,或者其他什么?请使用项目绑定事件。
假设我想在上面添加HotelName在激活时为蓝色,那么我们可以这样做:
型
再次强调,代码简洁,没有混乱的标记。我们将代码逻辑与标记分开。
现在的输出是这样的:
同样,请注意我们在这里甚至没有编写任何循环代码!
所以你想利用内置选项的中继器,并为一个GridView,listView,和中继器?
使用行数据绑定事件-它允许你格式化,甚至做数学,或者任何你想要的重复控件的每一行。正如你所看到的,它的平原简容易写代码作为奖励。