2017-02-28 35 views
0

我有一个数据列表控件,它里面我有两个表:如何在绑定到数据列表时从数据行中获取特定值? asp.net vb.net

<asp:DataList ID="dataListAccount" runat ="server">                
    <ItemTemplate >                
     <table runat ="server" id="tblAccountInfo">                              
      <tr> 
       <td>           
        Account Id: <%#Eval("AccountId")%> 
       </td> 
      </tr>                     
     </table> 
     <br>         
     <table runat="server" id ="tblAccountAmount" border="1">          
      <tr> 
       <td><%#Eval("AccountBalance", "{0:C}")%></td>           
      </tr>         
     </table>                            
    </ItemTemplate>                            
</asp:DataList> 

然后我填充数据列表控件:

'create datatable 
    Dim dataTableAccount As DataTable = New DataTable() 
    dataTableAccount.Columns.Add("AccountID") 
    dataTableAccount.Columns.Add("AccountBalance") 

    'populate data table 
    Dim dataRow As DataRow = dataTableAccount.NewRow() 
    dataRow(0) = 1 'Account ID 
    dataRow(1) = 100 'Balance on the Account with Id=1 
    dataTableAccount.Rows.Add(dataRow) 

    Dim dataRow1 As DataRow = dataTableAccount.NewRow() 
    dataRow1(0) = 2 'Account Id 
    dataRow1(1) = 0 'Balance on the Account with Id=2 
    dataTableAccount.Rows.Add(dataRow1) 
    dataListAccount.DataSource = dataTableAccount 
    dataListAccount.DataBind() 

在活动dataListAccount_ItemDataBound我想知道如何获取当前项目绑定的“AccounId”。

Private Sub dataListAccount_ItemDataBound(sender As Object, e As 
System.Web.UI.WebControls.DataListItemEventArgs) Handles 
dataListAccount.ItemDataBound 

Dim CurrentAccountId= ??????? 

End Sub 
+0

我想我找到了一种方法:Dim currentRowBindi ng As System.Data.DataRowView = CType(e.Item.DataItem,System.Data.DataRowView) Dim currentAccountId = currentRowBinding(“AccountId”) – pepe

回答

0

我想我发现这样做的一种方式:

Dim currentRowBinding As System.Data.DataRowView = CType(e.Item.DataItem, System.Data.DataRowView) 

Dim currentAccountId = currentRowBinding("AccountId") 
0

最好把值到标签

Account Id: <asp:label id="lblAccountID" runat="server" Text ='<%#Eval("AccountId")%>' /> 

在后面的代码,你可以很容易找到它使用

Dim CurrentAccountId = e.item.FindControl("lblAccountID") 
相关问题