ASP.NET转发器中的最后一项被代码隐藏所忽略

时间:2014-08-05 16:07:21

标签: asp.net vb.net datatable repeater

我有一个由DataTable提供的相当简单的中继器。 ItemTemplate中的元素设置为visible = false。在ItemDataBound事件中,我根据DataTable中的内容切换可见性等。除了转发器的最后一行获取其基本数据而不是ItemDataBound事件的任何更改之外,所有工作都很好。我可以逐步执行并观察它们在代码隐藏中的变化,但这些变化并没有反映在ASPX中。这只发生在最后一行(如果只返回一条记录,则只发生一行)。如果我返回四行,我会得到三行正确的按钮等,第四行只有未经修改的基本数据。

我在页面上创建了一个新的转发器,然后将其拆分为基础。我玩过ClientIdMode和.NET框架版本。我已经对标签格式进行了三重检查,删除了任何非必要的内容,并重新启动了建筑物中的每台计算机两次。我打开和关闭ViewState,我已经删除了IsPostBack检查,我已经重命名了转发器。我没有使用嵌套转发器(在项目的其他地方,但不在此页面上),我没有使用验证,我已经删除了所有的CommandArg东西。

我的ASPX:

<asp:repeater ID="rptrTEST" runat="server" Visible="true">
    <ItemTemplate>
        <span ID="TESTspanParticipantCSI" visible="true" runat="server">
            CSI: <%#Eval("MoodleID")%>
        </span>
        <br />
            <img src="../images/projectname/btn-add-subscriber-20.png"
                class="TESTbtnSubscriber TESTbtnParticipantSubscribe"
                ID="TESTbtnParticipantSubscribe"
                alt="Subscribe participant cell phone"
                visible="false"
                runat="server" />
            <asp:ImageButton
                ID="TESTbtnParticipantPause"
                CssClass="TESTbtnSubscriber TESTbtnPause TESTbtnParticipantPause"
                Visible="false"
                ImageURL="~/images/projectname/btn-pause-subscriber-20.png"
                runat="server" />
            <asp:ImageButton
                ID="TESTbtnParticipantPlay"
                CssClass="TESTbtnSubscriber TESTbtnPlay TESTbtnParticipantPlay"
                Visible="false"
                ImageURL="~/images/projectname/btn-play-subscriber-20.png"
                runat="server" />
            <asp:ImageButton
                ID="TESTbtnParticipantUnsubscribe"
                CssClass="TESTbtnParticipantUnsubscribe"
                Visible="false"
                ImageURL="../images/projectname/btn-delete-subscriber-20.png"
                runat="server" />
        <br />
    </ItemTemplate>
</asp:repeater>

我的VB:

Protected Sub rptrTEST_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptrTEST.ItemDataBound
    Dim objCSIs As New Reg_DB.clsSubscribers
    Dim dtCSIs As Data.DataTable = objCSIs.GetAllCSIs()
    Dim y As Integer = 0

    For Each rptItem As RepeaterItem In rptrTEST.Items
        Dim spanParticipantCSI As New HtmlGenericControl
        Dim btnParticipantSubscribe As HtmlImage = DirectCast(rptrTEST.Items(y).FindControl("TESTbtnParticipantSubscribe"), HtmlImage)
        Dim btnParticipantUnsubscribe As ImageButton = DirectCast(rptrTEST.Items(y).FindControl("TESTbtnParticipantUnsubscribe"), ImageButton)
        Dim btnParticipantPause As ImageButton = DirectCast(rptrTEST.Items(y).FindControl("TESTbtnParticipantPause"), ImageButton)
        Dim btnParticipantPlay As ImageButton = DirectCast(rptrTEST.Items(y).FindControl("TESTbtnParticipantPlay"), ImageButton)
        If dtSubscribersCooked.Rows(y)("ParticipantSubStatus") = "NONE" then
            spanParticipantCSI.InnerHtml = "subscribed"
            btnParticipantUnsubscribe.Visible = true
            btnParticipantPause.Visible = true
            btnParticipantSubscribe.Visible = false
            spanParticipantCSI.InnerHtml = "subscribed"
            btnParticipantPlay.Visible = false
        else
            spanParticipantCSI.InnerHtml = "whatever"
            btnParticipantUnsubscribe.Visible = true
            btnParticipantPause.Visible = true
            btnParticipantSubscribe.Visible = false
            spanParticipantCSI.InnerHtml = "sure"
            btnParticipantPlay.Visible = false
        end if
        y=y+1
    next
End Sub

所以我需要一位老牧师和一位年轻牧师,还是需要巫师?

1 个答案:

答案 0 :(得分:2)

这是因为转发器中的项目数量并不是您所期望的。循环遍历转发器集合中的每个项目时,它只知道已绑定的项目。由于您的上一个项目尚未绑定(目前正在绑定),因此它没有看到该项目。

您需要做的是完全摆脱For Each循环。然后只需编辑当前绑定的RepeaterItem。请注意我如何找到当前项目的每个控件:e.Item.FindControl()

Protected Sub rptrTEST_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptrTEST.ItemDataBound
    Dim objCSIs As New Reg_DB.clsSubscribers
    Dim dtCSIs As Data.DataTable = objCSIs.GetAllCSIs()    

    Dim spanParticipantCSI As New HtmlGenericControl
    Dim btnParticipantSubscribe As HtmlImage = DirectCast(e.Item.FindControl("TESTbtnParticipantSubscribe"), HtmlImage)
    Dim btnParticipantUnsubscribe As ImageButton = DirectCast(e.Item.FindControl("TESTbtnParticipantUnsubscribe"), ImageButton)
    Dim btnParticipantPause As ImageButton = DirectCast(e.Item.FindControl("TESTbtnParticipantPause"), ImageButton)
    Dim btnParticipantPlay As ImageButton = DirectCast(e.Item.FindControl("TESTbtnParticipantPlay"), ImageButton)

    If dtSubscribersCooked.Rows(y)("ParticipantSubStatus") = "NONE" then
        spanParticipantCSI.InnerHtml = "subscribed"
        btnParticipantUnsubscribe.Visible = true
        btnParticipantPause.Visible = true
        btnParticipantSubscribe.Visible = false
        spanParticipantCSI.InnerHtml = "subscribed"
        btnParticipantPlay.Visible = false
    else
        spanParticipantCSI.InnerHtml = "whatever"
        btnParticipantUnsubscribe.Visible = true
        btnParticipantPause.Visible = true
        btnParticipantSubscribe.Visible = false
        spanParticipantCSI.InnerHtml = "sure"
        btnParticipantPlay.Visible = false
    end if

End Sub

我不确定dtSubscribersCooked是什么或声明的位置。如果这是一个全局变量或其他东西,那么你可以选择将ParticipantSubStatus绑定到转发器上的HiddenField以获取它在每个ItemDataBound上的值。