When using DataList control, you may want to obtain the ID or other values from the item in the DataList ItemDataBound Event using the following snippets in asp.net and C#

There are two cases and you can get the values from the both case

1. If you have assigned a value in any controls e.g. label control during the DataBind of the DataList.

2. If you did not assign any value in any control and still want to obtain the value from any of the coloumn.

I am showing the snippets in both the .aspx file and in the ItemDatabount event of the code behind file

.aspx file

<asp:DataList ID="DataList1" runat="server" RepeatColumns="3" OnItemDataBound="DataList_ItemDataBound">
 <ItemTemplate>
 <asp:Label ID="Label_EmployeeName" runat="server" Text='<%#Eval("EmployeeName") %>'>
 </asp:Label>
 </ItemTemplate>
 </asp:DataList>
 <asp:Label ID="Label_Result" runat="server" Text=""></asp:Label>

———–
Code Behind File

protected void DataList1 _ItemDataBound(object sender, DataListItemEventArgs e)

{
 if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
 {

// Retrieving the Employee Name from the label control.
 Label EmployeeName = (Label)e.Item.FindControl("Label_EmployeeName");

Label_Result.Text = "Employee Name: " + EmployeeName.Text +"<br/>";

// Retrieving the Employee ID even if it is not assigned to any controls

int EmployeeID = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "EmployeeID").ToString());

Label_Result.Text += "Employee ID: " + EmployeeID.ToString();

}
 }