How to separate comma-separated DataItem field in GridView Eval?

here is my code -

 <asp:TemplateField HeaderText="HIGH RISK (10-12)" ItemStyle-HorizontalAlign="Center">
      <ItemTemplate>
           <asp:Label ID="lblHighrisk" runat="server" Text='<%# Eval("URANGE").ToString().Split('-')[0] %>' />
      </ItemTemplate>
 </asp:TemplateField>

but this gives a compile-time error. "Server tag is badly formed"

+3
source share
4 answers

Perhaps you have a problem with quotes in the second split?

to try

 Text='<%# Eval("URANGE").ToString().Split("-")[0] %>'

Does it help?

Update after comment Yes, that makes sense

Try changing the quote in this case.

 Text="<%# Eval'URANGE').ToString().Split('-')[0] %>"

Does it help?

+2
source

Try it without quotes:

<asp:Label ID="lblHighrisk" runat="server" 
     Text=<%# Eval("URANGE").ToString().Split('-')[0] %> />
+1
source

This will be fine when you convert the string "." to char and send it to the split method.

Text='<%# Eval("costIntegerPart").ToString().Split(Convert.ToChar("."))[0] %>'
+1
source

It will work

style='<%#Eval("cssHover").ToString().Split(new string[]{";"},StringSplitOptions.None)[0]%>'
0
source

All Articles