ItemDataBound issue

VBAHole22

Well-known member
Joined
Oct 21, 2003
Messages
432
Location
VA
I have a templated column in a datagrid that I wish to change to a different color based on a value in one of the fields. I put in a label control to try to get a handle on the field value at runtime but it always returns a blank string ("") instead of the value, which I can see in the datagrid after it renders

Here is my html:

Code:
<asp:TemplateColumn HeaderText="Status">
         <ItemTemplate>
			<asp:Label id="lblCurrentStatus" Runat="server">
				<%# DataBinder.Eval(Container.DataItem, "current_status") %>
            </asp:Label>
         </ItemTemplate>
         <EditItemTemplate>
            <asp:DropDownList runat="server" ID="Dropdownlist1"/>
         </EditItemTemplate>
      </asp:TemplateColumn>

And here is what I have in the ItemDataBound event

Code:
	private void SbeGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			try
			{
				if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
				{
					Label lblStat = (Label)e.Item.FindControl("lblCurrentStatus");
					string frylock = lblStat.Text;
					System.Diagnostics.Debug.WriteLine("val is " + frylock);

					if (frylock == "Current")
					{

						lblStat.ForeColor = System.Drawing.Color.Blue;
						e.Item.BackColor = System.Drawing.Color.Aqua;
					}
				}


What am i doing wrong here?
When I view the source of the rendered page my label id has been corrupted and looks like this now

<span id="MyGrid__ctl16_lblCurrentStatus">Current
 
Two things

1. Your label should be:
<asp:Label id="lblCurrentStatus" Runat="server"
Text=<%# DataBinder.Eval(Container.DataItem, "current_status") %>></asp:Label>

2. The formatting you are seeing is correct... MyGrid is the parent grid, ctl16 is the generic control for the cell, and lblCurrentStatus is your label, this when going back to the server will be transformed to MyGrid:_ctl16:lblCurrentStatus which I think you may be looking for??? You have to remember these are controls within a naming container and that is why they have this heiarchy.

Finally, if getting the label doesnt work you can always doing the following (in the OnItemDataBound event):

C#:
DataRowView drv = e.Item.DataItem as DataRowView
if(drv!=null)
{
      if (drv.Row["current_status"]=="Current")
     {
	lblStat.ForeColor = System.Drawing.Color.Blue;
	e.Item.BackColor = System.Drawing.Color.Aqua;
     }
}

Also keep in mind the DataGrid has a SelectItemIndex property that you can use if your just simply trying to keep track of the selected item, then you can set the selected item colors through properties or style sheet.
 
It was the Text = business that I was missing. Without it I guess you dont really have a label at all. At least you cannont test the text property if you arent assigning to text.
 
Glad I could help, and thanks for you thanks...been a bit battle fatigued lately and actually for once hearing some appreciation can do a lot for ones spirits.
 
Back
Top