0

i try to highlight record ...like when any one wants to upload documet then in repeater i try to highlight this new record and when user click on this document then this becomes as normal position means not highlight

<tr style='<%#if(DataBinder.Eval(Container.DataItem, "ViewedType")== 1) 
 { %> background-color: yellow;  <% }
   else { <% background-color: white;
    <%} %>'>

but it shows me error

CS1519: Invalid token 'else' in class, struct, or interface member declaration

Source Error:

Line 128:  style='<%#if(DataBinder.Eval(Container.DataItem, "ViewedType")== 1) 
Line 129:  { %> background-color: yellow;  <% }
Line 130:  else { <% background-color: white;
Line 131:  <%} %>'>
Line 132:  <%--<td>

how to solve?

drzaus
  • 24,171
  • 16
  • 142
  • 201
user3265377
  • 47
  • 1
  • 2
  • 10
  • 2
    It should be: else { %> – the_lotus Feb 05 '14 at 19:19
  • 1
    You should really be doing this logic in the code behind, before binding the values, rather than in the template. If the bound column is simply the color you can just bind the background color to that. – Servy Feb 05 '14 at 19:22

2 Answers2

1

You can't use control structures (like if statements) inside of a databinding expression tag (which is <%# %>), but you also can't use DataBinder inside of a regular tag (<% %>).

I'd recommend just using the conditional operater inline like this:

<tr style='background-color: <%# (bool) DataBinder.Eval(Container.DataItem, "ViewedType") ? "yellow" : "white" %>'>
John Gibb
  • 10,603
  • 2
  • 37
  • 48
1

Try

<tr style='background-color: <%# ChooseColor((int)DataBinder.Eval(Container.DataItem, "ViewedType")) #>;'>

where

protected string ChooseColor(int viewedType){
    if (viewedType == 1) return "yellow"; else return "white";
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Nice option too, I think you'll need to add a cast in the markup since DataBinder.Eval returns an object. Something like `<%# ChooseColor((int) DataBinder....` – John Gibb Feb 05 '14 at 19:50
  • @JohnSaunders the code which is protected string is in .aspx or in .aspx.cs – user3265377 Feb 05 '14 at 20:27
  • .aspx.cs. There should be only a bare minimum of C# code in the .aspx file. Nothing more than simple data binding expressions. – John Saunders Feb 05 '14 at 20:49