简体   繁体   中英

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

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?

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" %>'>

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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM