【问题标题】:Repeater controls returning null中继器控件返回 null
【发布时间】:2012-10-16 17:47:58
【问题描述】:

我目前有一个数据读取器作为绑定到转发器的数据源。如此处所示:

Try
            myConnection.Open()
            sqlCommand = New SqlCommand(sqlText, myConnection)
            dr = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection)
            cropPlanRepeater.DataSource = dr
            cropPlanRepeater.DataBind()
 Catch ex As Exception

 End Try

这会启动数据绑定事件,如下所示:

Public Sub CropPlanRepeater_ItemDataBound(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles cropPlanRepeater.ItemDataBound

    Dim rptI As RepeaterItem = e.Item
    Dim AmttoSell = TryCast(rptI.FindControl("amtToSell"), Label)
    AmttoSell.Text = AmttoSell
End Sub

由于某种原因,这个 findcontrol 方法不断返回“null”或“nothing”我试图查看我的控件层次结构中是否存在缺陷等等,但我找不到任何东西。

这里的 HTML 看看它是如何工作的:

<asp:Repeater ID="cropPlanRepeater" runat="server" Visible="false">
            <HeaderTemplate>
                <tr align="center" class="top-rpt-head">
                    <th scope="col">
                        Crop
                    </th>
                    <th scope="col">
                        Production
                    </th>
                    <th scope="col">
                        Amount to Sell
                    </th>
                    <th scope="col">
                        Breakeven
                    </th>
                    <th scope="col">
                        Target Price
                    </th>
                    <th scope="col">
                        Target Revenue
                    </th>
                    <th scope="col">
                        Feed Value
                    </th>
                    <th scope="col">
                        Other Income
                    </th>
                    <th scope="col">
                        Expenses
                    </th>
                    <th scope="col">
                        Profit
                    </th>
                </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr align="center">
                    <td>
                    <!--- this needs to be fixed- incorrect methodology(should be done in the codebehind)----->
                        <%# (DataBinder.Eval(Container.DataItem, "Crop_Name"))%>
                    </td>
                    <td>
                    <!--- this needs to be fixed- incorrect methodology(should be done in the codebehind)----->
                        <%# (DataBinder.Eval(Container.DataItem, "Acres_D") * DataBinder.Eval(Container.DataItem, "Yield_D") + DataBinder.Eval(Container.DataItem, "Acres_I") * DataBinder.Eval(Container.DataItem, "Yield_I"))%>
                    </td>
                    <td>

                    </td>
                 </tr>
                    <asp:label runat="server" ID="AmtToSell"></asp:Label>

                    <asp:Literal runat="server" ID="Yield_I" Text='<%#Eval("Yield_I") %>' Visible="false" />
                    <asp:Literal runat="server" ID="Yield_D" Text='<%#Eval("Yield_D") %>' Visible="false" />
                    <asp:Literal runat="server" ID="Acres_I" Text='<%#Eval("Acres_I") %>' Visible="false" />
                    <asp:Literal runat="server" ID="Acres_D" Text='<%#Eval("Acres_D") %>' Visible="false" />
                    <asp:Literal runat="server" ID="Storage" Text='<%#Eval("Storage") %>' Visible="false" />
                    <asp:Literal runat="server" ID="Fed" Text='<%#Eval("Fed") %>' Visible="false" />
                    <asp:Literal runat="server" ID="Bartered" Text='<%#Eval("Bartered") %>' Visible="false" />
                    <asp:Literal runat="server" ID="Profit_I" Text='<%#Eval("ProfAcre_I") %>' Visible="false" />
                    <asp:literal runat="server" ID="Profit_D" Text='<%#Eval("ProfAcre_D") %>' Visible="false" />         
            </ItemTemplate>
        </asp:Repeater>

我尝试将标签放在表格的内部和外部,但无济于事。我不确定我的 HTML 或代码隐藏是否有问题。我需要通过代码隐藏中的计算来操作该标签“amttosell”。 (我会做类似 amttosell.text = 从数据库获取的数据/发布在上面的隐藏标签上的一些计算

任何帮助将不胜感激。

【问题讨论】:

    标签: asp.net vb.net data-binding repeater


    【解决方案1】:

    CropPlanRepeater_ItemDataBound 方法处理页眉、页脚和项目。对于您拥有的代码,您需要将其包装在 if 块中,检查项目类型是否为 Item 或 AlternateItem。

    Public Sub CropPlanRepeater_ItemDataBound(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles cropPlanRepeater.ItemDataBound
    
    Dim rptI As RepeaterItem = e.Item
    
    if rptI.ItemType = ListItemType.Item OR rptI.ItemType = ListItemType.AlternatingItem then
        Dim AmttoSell = TryCast(rptI.FindControl("amtToSell"), Label)
        AmttoSell.Text = AmttoSell
    
    end if
    End Sub
    

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound.aspx#Y200

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-28
      • 2020-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-08
      相关资源
      最近更新 更多