asp.net 如何获取Repeater的当前ItemIndex在代码后面

ktca8awb  于 11个月前  发布在  .NET
关注(0)|答案(2)|浏览(111)

虽然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") %>

4ioopgfo

4ioopgfo1#

你不能在模板化控件的模板中使用“代码渲染块”。如果你只关心你的标记块是否会被渲染成HTML,而不管它是否被评估,你可以使用控件的Visible/visible属性。

<ItemTemplate>
    <div runat="server" visible=<%# Container.ItemIndex = 2 %>>
        TRUE/some longer HTML here/
    </div>
    <div runat="server" visible=<%# Container.ItemIndex <> 2 %>>
        false/some longer HTML here/
    </div>
</ItemTemplate>

字符串

i7uaboj4

i7uaboj42#

好的,只需按下一个按钮,然后拿起中继器行。
所以,假设我们有这个repeater标记:

<asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
          <div style="border-style:solid;color:black;width:290px;float:left;padding:10px">

            <div style="padding:5px;text-align:right">
                Hotel Name: <asp:TextBox ID="txtHotelName" runat="server" Text ='<%# Eval("HotelName") %>' />
                <br />
                First Name: <asp:TextBox ID="txtFirst" runat="server" Text ='<%# Eval("FirstName") %>' />
                <br />
                Last Name: <asp:TextBox ID="txtLast" runat="server" Text ='<%# Eval("LastName") %>'  />
                <br />
                City: <asp:TextBox ID="City" runat="server" Text ='<%# Eval("City") %>'  />
                <br />
                Province: <asp:TextBox ID="Province" runat="server" Text ='<%# Eval("Province") %>'/>
                <br />
                Active: <asp:CheckBox ID="chkActive" runat="server" Checked = '<%# Eval("Active") %>'/>
                <br />
            
                <asp:Button ID="cmdRowC" runat="server" Text="Row Click" OnClick="cmdRowC_Click"/>
            </div>
           </div>
           <div style="clear:both;height:5px"></div>
        </ItemTemplate>
       </asp:Repeater>

字符串
和我们的代码后面来填充这个中继器:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If IsPostBack = False Then
        LoadGrid()
    End If

End Sub

Sub LoadGrid()

    Using cmdSQL As New SqlCommand("SELECT * FROM tblHotels ORDER BY HotelName",
                    New SqlConnection(My.Settings.TEST3))
        cmdSQL.Connection.Open()
        Repeater1.DataSource = cmdSQL.ExecuteReader
        Repeater1.DataBind()
    End Using

End Sub


我们现在有了这个:
x1c 0d1x的数据
现在是按钮代码。注意我是如何放入一个标准按钮的。
但是,我们现在不能双击按钮来自动连接并创建事件。
但是你可以这样做来创建一个事件:
在按钮代码标记中,键入OnClick=
当你点击“="时,intel-sense会弹出一个对话框,让你创建一个按钮点击事件,如下所示:



所以,现在点击创建新事件。它“似乎”什么也没发生,但如果我们翻到代码背后,你会发现点击事件存根。
所以,现在在我们的点击事件代码中,我们可以很容易地获取当前的重复行,值,甚至是索引。
代码如下:

Protected Sub cmdRowC_Click(sender As Object, e As EventArgs)

    Dim cBtn As Button = sender
    Dim rRow As RepeaterItem = cBtn.Parent

    Debug.Print("Row clicked = " & rRow.ItemIndex)

    Debug.Print("First Name = " & DirectCast(rRow.FindControl("txtFirst"), TextBox).Text)
    Debug.Print("Last Name = " & DirectCast(rRow.FindControl("txtLast"), TextBox).Text)
    Debug.Print("Hotel Name = " & DirectCast(rRow.FindControl("txtHotelName"), TextBox).Text)

End Sub


产出:

Row clicked = 1
First Name = Darcy
Last Name = Caroll
Hotel Name = Athabasca Hotel


因此,只需放入一个plane jane按钮。当您单击它时,事件存根就会运行,正如您所看到的,我们选择了“Repeater row item”。
从这一行,我们可以得到行索引,当然还可以使用find control从这一行中取出任何其他控件值。
例如:

Dim txtHotel as TextBox = rRow.FindControl("txtHotel")

debug.print ("Hotel name = " & txtHotel.Text)


所以,一旦你有了repeater行,你就不需要一些container.ItemIndex表达式了,因为行项允许你通过以下方式获取给定的索引行:

rRow.ItemIndex


因此,ItemIndex是可用的,并且无论您是否在标记中包含或使用conttin.ItemIndex都是可用的。
在标记中几乎不需要对这些代码进行处理。
请注意上面是多么的干净和简单。我建议你做出巨大的努力来避免像你正在做的那样将vb代码倾倒在标记中。如果你甚至想转换为c#,或者只是维护这些代码?把这些代码放在代码后面的区域-而不是标记中。
如上所述,对于重复数据等,您可以使用Repeater,甚至我经常使用listview -因为它允许自动创建重复布局,而无需编写循环代码。

好了,现在我们已经完成了,假设我们想在中继器中输入一条消息,

This hotel is Active


This hotel is NOT active!


好吧,在我们的标记中,我们可以添加这样的标签:
现在,在repeater属性表中,双击此处以创建项目数据绑定事件:



现在很简单,我们可以像这样为每一行添加代码逻辑:

Protected Sub Repeater1_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles Repeater1.ItemDataBound

    Dim rRow As RepeaterItem = e.Item

    Dim ckbox As CheckBox = rRow.FindControl("chkActive")

    Dim MyLabel As Label = rRow.FindControl("Label1")

    If ckbox.Checked Then
        MyLabel.Text = "This Hotel is Active!!!"
    Else
        MyLabel.Text = "Not active!"
    End If

End Sub


现在的输出是这样的:



所以,再一次,要对repeater进行条件格式化,为活动酒店添加颜色,或者其他什么?请使用项目绑定事件。
假设我想在上面添加HotelName在激活时为蓝色,那么我们可以这样做:

Protected Sub Repeater1_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles Repeater1.ItemDataBound

    Dim rRow As RepeaterItem = e.Item

    Dim ckbox As CheckBox = rRow.FindControl("chkActive")

    Dim MyLabel As Label = rRow.FindControl("Label1")

    If ckbox.Checked Then
        MyLabel.Text = "This Hotel is Active!!!"
    Else
        MyLabel.Text = "Not active!"
    End If

    Dim txtHotel As TextBox = rRow.FindControl("txtHotelName")

    If ckbox.Checked Then
        txtHotel.BackColor = Drawing.Color.AliceBlue
    End If
End Sub


再次强调,代码简洁,没有混乱的标记。我们将代码逻辑与标记分开。
现在的输出是这样的:

同样,请注意我们在这里甚至没有编写任何循环代码!
所以你想利用内置选项的中继器,并为一个GridView,listView,和中继器?
使用行数据绑定事件-它允许你格式化,甚至做数学,或者任何你想要的重复控件的每一行。正如你所看到的,它的平原简容易写代码作为奖励。

相关问题