468,532 Members | 1,703 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,532 developers. It's quick & easy.

Get data value(s) from a DataList control

I've tried various ways to get data value(s) from a DataList control (with same results for ListView and FormView). These are my attempts:

Expand|Select|Wrap|Line Numbers
  1.     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.         Dim dli As DataListItem
  3.         For Each dli In DataList1.Items
  4.             Dim theValue As String = CType(DataList1.FindControl("AttendeeMailLabel"), Label).Text
  5.             MsgBox(theValue)
  6.         Next
  7. End Sub
With result: Object reference not set to an instance of an object

I've also tried:

Expand|Select|Wrap|Line Numbers
  1.         Dim itemValue As String
  2.         Dim lvi As ListViewItem
  3.         For Each lvi In ListView1.Items
  4.             itemValue = lvi.Text
  5.             'also tried, with same result: itemValue = lvi.SubItems(0).Text
  6.             MsgBox(itemValue)
  7.         Next
  8. End Sub
With result: "'Text' is not a member of System.Web.UI.WebControls.ListViewItem"

Any ideas/suggestions?

This is the script:

Expand|Select|Wrap|Line Numbers
  1.     <form id="form1" runat="server">
  2.     <div>
  3.  
  4.     </div>
  5.     <br />
  6.     <br />
  7.     <asp:ListView ID="ListView1" runat="server" DataKeyNames="AttendeeID" 
  8.         DataSourceID="SqlDataSource1">
  9.         <ItemTemplate>
  10.             <tr style="">
  11.                 <td>
  12.                     <asp:Label ID="AttendeeEmailLabel" runat="server" 
  13.                         Text='<%# Eval("AttendeeEmail") %>' />
  14.                 </td>
  15.             </tr>
  16.         </ItemTemplate>
  17.         <AlternatingItemTemplate>
  18.             <tr style="">
  19.                 <td>
  20.                     <asp:Label ID="AttendeeEmailLabel" runat="server" 
  21.                         Text='<%# Eval("AttendeeEmail") %>' />
  22.                 </td>
  23.             </tr>
  24.         </AlternatingItemTemplate>
  25.         <EmptyDataTemplate> ... </EmptyDataTemplate>
  26.         <InsertItemTemplate> ...  </InsertItemTemplate>
  27.         <LayoutTemplate> ... </LayoutTemplate>
  28.         <EditItemTemplate> ... </EditItemTemplate>
  29.         <SelectedItemTemplate>
  30.             <tr style="">
  31.                 <td>
  32.                     <asp:Label ID="AttendeeEmailLabel" runat="server" 
  33.                         Text='<%# Eval("AttendeeEmail") %>' />
  34.                 </td>
  35.             </tr>
  36.         </SelectedItemTemplate>
  37.     </asp:ListView>
  38.     <asp:Button ID="Button1" runat="server" Text="Button" />
  39.     <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
  40.         ConnectionString="<%$ ConnectionStrings:ActivitiesConnectionString %>" 
  41.         SelectCommand="SELECT [AttendeeEmail], [AttendeeID] FROM [Attendees] WHERE ([ActivityID] = @ActivityID)">
  42.         <SelectParameters>
  43.             <asp:Parameter DefaultValue="43" Name="ActivityID" Type="Int32" />
  44.         </SelectParameters>
  45.     </asp:SqlDataSource>
  46.     </form>
Feb 12 '09 #1
1 8657
Expand|Select|Wrap|Line Numbers
  1. Dim theValue As String = DirectCast(dli.FindControl("AttendeeMailLabel"), Label).Text
Thanks to Qin Dian Tang - MSFT at forums.asp.net

-Bradley
Feb 14 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Yoshitha | last post: by
reply views Thread by Anil Kulkarni | last post: by
3 posts views Thread by Colin Young | last post: by
5 posts views Thread by Stephanie_Stowe | last post: by
4 posts views Thread by regaliel | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.