15

I've a asp.net linkbutton inside asp.net repeater control which renders multiple link buttons. I want to set the style of each and every linkbutton dynamically.

I'm trying

style="color:#6D7B8D;font-size:<%# DataBinder.Eval(Container.DataItem, "Title")%>;"

But i'm getting "The server tag is not well formed" error.

Any ideas?

NLV
  • 21,141
  • 40
  • 118
  • 183

4 Answers4

25

My understanding is that using server tags for attributes requires that the server tag be used for the entire attribute value. Try changing it to this:

style='<%# "color:#6D7B8D;font-size:" + DataBinder.Eval(Container.DataItem, "Title") + ";" %>'

Notice how the entire style attribute is being constructed in C# code between the server tags.

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
  • In my point of view the apostrophes are unnecessary: just type style=<%# "color:#6D7B8D;font-size:" + DataBinder.Eval(Container.DataItem, "Title") + ";" %> – Franziee Jan 14 '15 at 09:23
4

Write it like that:

style='color:#6D7B8D;font-size:<%# DataBinder.Eval(Container.DataItem, "Title")%>;'

With single quotes instead of double quotes around the style

Amr Elgarhy
  • 66,568
  • 69
  • 184
  • 301
0

We can also use trenary operator within it-- to custom the font size

style='<%# "color:#6D7B8D;font-size:"Convert.ToInt32(DataBinder.Eval(Container.DataItem, "fontSize"))<5?"40":"30");" %>'
Trikaldarshiii
  • 11,174
  • 16
  • 67
  • 95
Lijin Durairaj
  • 4,910
  • 15
  • 52
  • 85
0

Remove

<%# Databinder.Eval(Container.DataItem, "Title") %>

To:

Text='<%# Databinder.Eval(Container.DataItem, "Title") %>'
GregD
  • 6,860
  • 5
  • 34
  • 61