【问题标题】:Getting Repeater values from code behind从后面的代码中获取中继器值
【发布时间】:2017-09-28 19:22:25
【问题描述】:

我想在按下按钮后从后面的代码中获取转发器值。但在我的代码隐藏中,中继器总是出现“0”。我的意思是什么。

我是我在 aspx 页面中的转发器:

<asp:Repeater ID="MyRepeater" runat="server" DataSourceID="SqlDataSourceU">
    <HeaderTemplate>
        <table>
            <tr>
                <th>Car Type</th>
                <th>Cars</th>
            </tr>
    </HeaderTemplate>

    <ItemTemplate>
        <tr>
            <td>
                <asp:Label runat="server" ID="lblUnitTypeValue" Text='<%# Eval("CarTypeName") %>' />
            </td>
            <td>
                <th>
                    <asp:DropDownList ID="ddlCars"
                        runat="server"
                        DataSourceID="SqlDataSourceViews"
                        DataTextField="problemLocationName"
                        DataValueField="problemLocationId"
                        Width="212px">
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDataSourceViews"
                        runat="server"
                        ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
                        SelectCommand="SELECT [problemLocationId],[problemLocationName] FROM [ProblemLocation]"></asp:SqlDataSource>
                </th>
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>

<asp:SqlDataSource
        ConnectionString="<%$ ConnectionStrings:QfsAdminConnectionString %>"
        ID="SqlDataSourceU" runat="server"
        SelectCommand="SELECT [CarTypeName] FROM [CarType]">
</asp:SqlDataSource>

这是我的代码隐藏。它不会进入我的foreach 循环,因为它总是一无所有。但这不是真的,我的中继器中有值。 所以我想按下按钮并从我的中继器中获取值。

Protected Sub btnSaveDefaultView_Click(sender As Object, e As EventArgs) Handles btnSaveDefaultView.Click
    Dim itens As Repeater = CType(ViewDefaultView.FindControl("RepeaterUnitTypes"), Repeater)

    For Each item In itens.Items

    Next
End Sub

【问题讨论】:

  • 我不确定,但这可能有效:For each item as (your item type) in itens 在此处阅读有关For Each...Next 的更多信息:docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/…
  • 你的意思是变量“itens”是Nothing吗?
  • 是的,我的意思是变量是Nothing。
  • 我对 vb.net 了解不多,但在 c# 中,您必须将数据绑定到中继器。你在这里做过吗?
  • 天哪,这里有很多误解。您是否尝试只使用一个中继器一次编辑所有记录?主啊!

标签: .net vb.net webforms repeater


【解决方案1】:

使用此代码从转发器获取值,无需查找转发器:

For Each rItem As RepeaterItem In MyRepeater.Items
  Dim lbl As Label = DirectCast(rItem.FindControl("lblUnitTypeValue"), Label)
  If Not IsNothing(lbl) Then
    Dim value As Integer = Convert.ToInt32(lbl.Text)
  End If
Next

【讨论】:

    【解决方案2】:

    去这里的马棚!

    将Repeater生成的html解析为JSON '[CarType]' 对象并将其ajax到服务器。

    VOALÁ

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-15
      • 2016-05-21
      • 2011-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多