Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

 

<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

 

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</span>

Wanna-Be C# Superstar
Posted

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 doesn't work you can always doing the following (in the OnItemDataBound event):

 

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.

Posted
It was the Text = ' ' business that I was missing. Without it I guess you don't really have a label at all. At least you cannont test the text property if you aren't assigning to text.
Wanna-Be C# Superstar
Posted
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 one's spirits.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...