skip to Main Content

I have a label in a gridview, and I want to put an awesome icon font inside the label according to the value of a field, my code is:

<asp:Label runat="server" Text='<%# string.Format("<i class='fa fa-{0}'></i>", Eval("[TypeIconFF]")) %>' EncodeHtml="false" />

But it gives me an exception of type System.Web.HttpException (Wrong server label)

I think the error may be generated by single quotation marks

Does anyone have any idea how to solve it?

Thanks in advance

2

Answers


  1. The problem is the second set of single quotes around fa fa-{0}.

    Change them to double quotes and escape them and it will work.

    <asp:Label runat="server" Text='<%# string.Format("<i class="fa fa-{0}"></i>", Eval("[TypeIconFF]")) %>' EncodeHtml="false" />
    
    Login or Signup to reply.
  2. Try:

    <asp:Label ID="Label2" runat="server" 
      Text='<%# $@"<i class=""fa fa-{Eval("TypeIconFF")}""></i>"  %>'
     >
    </asp:Label>
    

    Not sure if you need the [], but then this:

    <asp:Label ID="Label2" runat="server" 
      Text='<%# $@"<i class=""fa fa-{Eval("[TypeIconFF]")}""></i>"  %>'
     >
    </asp:Label>
    

    you also should not need the html encode option.

    So, with this grid:

    I also like the fa-lg (larger).

    So this gv:

    <asp:GridView ID="GridView1" runat="server" ShowHeaderWhenEmpty="true"
        AutoGenerateColumns="False" DataKeyNames="ID"  CssClass="table table-hover" Width="52%">
        <Columns>
            <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
            <asp:BoundField DataField="LastName" HeaderText="LastName"  />
            <asp:BoundField DataField="HotelName" HeaderText="HotelName" />
            <asp:BoundField DataField="Description" HeaderText="Description"  />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" 
                        Text='<%# $@"<i class=""fa fa-{Eval("FA")} fa-lg""></i>"  %>'>
                    </asp:Label>
    
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    

    And code :

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GridView1.DataSource = General.MyRst("SELECT * FROM tblhotelsA");
                GridView1.DataBind();
    
            }
        }
    

    and I get/see this:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search