Repeater中的Container.ItemIndex - 渲染问题

时间:2013-04-30 11:25:19

标签: c# asp.net

我在Repeater中有这行代码: -

<div class="paymentOptionsForm" onclick="$('.rbToken<%# Container.ItemIndex+1 %>>input').attr('checked', 'checked'); changeActiveType('token<%# Container.ItemIndex+1 %>', this);">

在HTML中渲染得很好,如: -

<div class="paymentOptionsForm" onclick="$('.rbToken1>input').attr('checked', 'checked'); changeActiveType('token1', this);">

但是(在同一个中继器中)

<div id="divTokenDropDown" class="tokenDecision<%# Container.ItemIndex+1 %>" runat="server">

呈现为: -

<div id="ctl00_plcContentMain_CtlToken_rptcard_ctl00_divTokenDropDown" class="tokenDecision&lt;%# Container.ItemIndex+1 %>" style="display:block;">

我不确定为什么会以这种方式呈现,除了它可能与runat =“server”有关,但我需要把它留在这里。

1 个答案:

答案 0 :(得分:1)

试试这样:

<div id="divTokenDropDown" 
   class='<%# string.Format("tokenDecision{0}", Container.ItemIndex+1) %>' 
   runat="server">

还有一些其他表达式也可以使用(比如string.Concat)。

我只是在这里推测,但我猜测runat="server"属性意味着解析器将标记视为System.Web.UI.HtmlControls.HtmlGenericControl,(我进一步猜测)允许您将属性绑定到表达式,但不能将文字与表达式混合作为属性值。您使用的第一个标记不是服务器控件,因此解析器甚至不将其解释为标记,就像它需要删除表达式值的位置一样。