0

I have this aspx code:

<asp:TemplateField HeaderText="Name"  SortExpression="Firmierung">
   <ItemTemplate>
      <asp:HyperLink ID="HyperLink" runat="server" NavigateUrl='<%#Eval("Id", "DetailInfo.aspx?Id={0}") %>' Text='<%#Bind("Name") %>' />
   </ItemTemplate>
</asp:TemplateField>

I want to add an if condition so that I can use operand + with Text, something like this:

<asp:TemplateField HeaderText="Name"  SortExpression="Firmierung">
    <ItemTemplate>
        <% if(Condition is true) { %>
           <asp:HyperLink ID="HyperLink" runat="server" NavigateUrl='<%#Eval("Id", "DetailInfo.aspx?Id={0}") %>' Text='<%#Bind("Name") + ("Active") %>' />
        <% } else { %>
           <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%#Eval("Id", "DetailInfo.aspx?Id={0}") %>' Text='<%#Bind("Name") %>' />
        <% } %> 
    </ItemTemplate>
</asp:TemplateField>

I don't know how to use operand + in this case. Any help appreciated. Thanks!

Ragnarsson
  • 1,715
  • 7
  • 41
  • 74

2 Answers2

0

You can't do it with Bind. Although with Eval you can add some C# code with it, with Bind it is not possible. Eval is really a method call but Bind is just a declaration that turned by the framework to some binding code, and its format has to be Bind("FieldName") (You can add formatting though).

The Hyperlink Text property is not editable by the client so Eval should be enough and you can write it in one row.

<asp:HyperLink ID="HyperLink" runat="server" 
      NavigateUrl='<%#Eval("Id", "DetailInfo.aspx?Id={0}") %>' 
      Text='<%# Eval("Name") + (Condition ? + Eval("Active") : "") %>' />
Amiram Korach
  • 13,056
  • 3
  • 28
  • 30
  • so, as your suggestion, I did like : Text='<%# (bool) Eval("Active") ? Bind("Name") + "Active" : Bind("Name") %>' /> and I get this error : The name 'Bind' does not exist in the current context. Is there something wrong? – Ragnarsson Sep 20 '12 at 15:34
  • You can't do this. You can't add code to `Bind`. ou can use `Eval` only. What exactly are you trying to do? – Amiram Korach Sep 20 '12 at 20:02
  • @Magnus, this is not my answer. I'm rolling back. If you think otherwise please comment or write your own answer. – Amiram Korach Sep 20 '12 at 20:04
  • @AmiramKorach sorry about that, thought is was a syntax error. – Magnus Sep 20 '12 at 21:40
  • @AmiramKorach: I resolved this problem, my answer is above. Thanks for your help :) . What I wanted was to check if this Name is Active (bool), then I print Hyperlink's Text is Name (Active), otherwise, Text is only Name. :) – Ragnarsson Sep 21 '12 at 10:55
0

Try this one

Text='<%# String.Format({0}{1},Eval("Name"),Eval("Active")) %>' 

OR

Text='<%# String.Format({0}{1},Bind("Name"),Bind("Active")) %>'

For more help visit the link:

HyperLink with NavigateUrl with Eval(). Where is the mistake?

Community
  • 1
  • 1
Pushpendra
  • 814
  • 1
  • 6
  • 17
  • Hi, thanks your answer, that's what I was looking for, but there's a little error syntax in your answer, we need "" for {0}{1}. So, this is my code, worked like a charm : Text='<%# String.Format("{0} ({1})", Eval("Name"), Eval("Active")) %>' – Ragnarsson Sep 21 '12 at 10:51