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.
Thanks
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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;
}
}
Method 2
I think what you’re after is the ListViewDataItem.DataItem
Method 3
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);
}
}
Method 4
<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.*/
}
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0