0

I have GridView to display data as Label

<ItemTemplate>
     <asp:Label ID="lblIsActive" runat="server" Text='<%# GetIcon((String)Eval("IS_ACTIVE"))%>' SkinID='<%# GetSkinId((String)Eval("IS_ACTIVE"))%>' />
</ItemTemplate>

c#

protected string GetSkinId(string name)
    {
        if (name == "Y")
        {
            return "sknActive";
        }
        else
            return "sknInactive";
    }

but I get error I can't set SkinID Programmatically, any idea how I can allow SkinID in code behind?

Updated I decide to not make SkinID, so I'm doing this

<ItemTemplate>
   <asp:Label ID="lblIsActive" runat="server" Text='<%# GetIcon((String)Eval("IS_ACTIVE"))%>' 
              ForeColor='<%# GetColor((String)Eval("IS_ACTIVE"))%>' />
</ItemTemplate>

And my function on c# to get color

protected string GetColor(string name)
{
    if (name == "Y")
    {
        return "#99099";
    }
    else
        return "#03211";
}

I get error that

string can not convert to System.Drawing

Rahul Singh
  • 21,585
  • 6
  • 41
  • 56
  • @Irshad `The 'SkinId' property can only be set in or before the Page_PreInit event for static controls. For dynamic controls, set the property before adding it to the Controls collection.` – sarah khaled Dec 14 '15 at 08:17

2 Answers2

1

The error message tells you what you need to do: move the initializing of the SkinID property to the Page_PreInit stage of the page lifecycle.

Basically, this entails adding the following event handler to your code behind:

protected void Page_PreInit(object sender, EventArgs e)
{
    lblIsActive.SkinId = GetSkinID(IS_ACTIVE); // no need for eval here
}
Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
1

The error message is self explanatory. Based on the data source you have the control in your gridview is dynamically getting created and after this it is trying to set the SkinId property and thus the error.

You can achieve this when the row is getting created in your gridview. Yes you can use the RowCreated event of gridvew like this:-

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Label lblIsActive = e.Row.FindControl("lblIsActive") as Label;
        if(lblIsActive.Text == "Y")
            lblIsActive.SkinID = "sknActive";
        else
            lblIsActive.SkinID = "sknInactive";
    }
}

Please note that this won't work in RowDataBound event since this event is fired after the row is created and when a data row is bound to data.

Update:

First of all your assumption is wrong that we are looping actually we are not. We are simply handling the event which is raised by the gridview control. Anyways since now you have changed your mind and switched to ForeColor approach the problem with your code is that the ForeColor property expects a System.Drawin.Color enum but your passing a string thus the error. For correctint you will have to return Color instead of string like this:-

protected Color GetColor(string name)
{
   if (name == "Y")
       return  Color.Red;
   else
       return Color.Green;
 }

Here I am returning sample colors but you need to replace them with actual intended colors. If you just have the hex string and not sure about the Color enum value then you can use the method mentioned in this answer to do so.

Community
  • 1
  • 1
Rahul Singh
  • 21,585
  • 6
  • 41
  • 56
  • Good, but I should do **loop** which is not good, so I think to make function to return **Forecolor**, I will update my code, see it and give me your feedback – sarah khaled Dec 14 '15 at 14:55