Tutorial :Get Data Value From ListView ItemDataBound


I'm sure I've done this before but really cant remember how.

In the ItemDataBound event of a ListView I need to get the actual data value. I cant seem to find it in the ListViewItemEventArgs object that gets passed in.



I think what you're after is the ListViewDataItem.DataItem


Use the ListViewDataItem in the ItemDataBound event:

protected void yourListView_ItemDataBound(object sender, ListViewItemEventArgs e)  {      ListViewDataItem dataItem = (ListViewDataItem)e.Item;        if (e.Item.ItemType == ListViewItemType.DataItem)      {          YourDataSource yourDataSource= (YourDataSource )dataItem.DataItem;                    }    }  


protected void Score_ItemDataBound(object sender, Telerik.Web.UI.RadListViewItemEventArgs e)  {      if (e.Item is RadListViewItem)      {          RadListViewDataItem item = e.Item as RadListViewDataItem;          object dataItem = ((System.Data.DataRowView)(((RadListViewDataItem)e.Item).DataItem)).Row.ItemArray[2].ToString();          string raetest = Convert.ToString(dataItem);      }  }  


<asp:ListView ID="ContactsListView"           DataSourceID="ContactsDataSource"           ConvertEmptyStringToNull="true"                  OnItemDataBound="ContactsListView_ItemDataBound"          runat="server">          <LayoutTemplate>            <table cellpadding="2" width="680px" border="0">              <tr style="background-color: #ADD8E6" runat="server">                  <th runat="server">First Name</th>                  <th runat="server">Last Name</th>                  <th runat="server">E-mail Address</th>              </tr>              <tr runat="server" id="itemPlaceholder" />            </table>            <asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">              <Fields>                <asp:NumericPagerField ButtonCount="10" />               </Fields>            </asp:DataPager>          </LayoutTemplate>          <ItemTemplate>            <tr style="background-color: #CAEEFF" runat="server">              <td>                <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />              </td>              <td>                <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />              </td>              <td>                <asp:Label ID="EmailAddressLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />              </td>            </tr>          </ItemTemplate>        </asp:ListView>  

Server side

protected void ContactsListView_ItemDataBound(object sender, ListViewItemEventArgs e)        {          if (e.Item.ItemType == ListViewItemType.DataItem)          {            // Display the e-mail address in italics.            Label EmailAddressLabel = (Label)e.Item.FindControl("EmailAddressLabel");            // EmailAddressLabel.Font.Italic = true;            string valueoftheControl = EmailAddressLabel.Text;              /* you have to get the value like this.                If its a dropdown or any other use their                corresponding property to get the value.*/          }        }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »