从后面的代码获取Repeater值

问题描述:

我想在按下按钮后从代码背后获得中继器值。但在我的代码背后,转发器总是“0”。我的意思是没有。从后面的代码获取Repeater值

我是我的中继器在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 
+0

我不知道,但是这可能工作:'对于每个项目为(您的项目型)itens'了解更多关于'对于每个... Next'这里:https://开头的文档。 microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/for-each-next-statement – Subaz

+0

你的意思是变量“itens”是什么? –

+0

是的,我的意思是变量是Nothing。 –

使用此代码从中继器获取值,也没有必要找一个中继器:

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 

去骑马Awnser这里!

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

VOALÁ