How come this ins't working for me? If/Else repater string

Dcode warner asked on March 13, 2017 17:35

  <%# ((Eval("NodeOwner").ToString() != "Britney Spears") ? "Britney Spears true" : "") %>
  <%# ((Eval("NodeOwner").ToString() != "Britney Spears") ? "Britney Spears Web Desinger true" : "") %>
  <%# ((Eval("NodeOwner").ToString() != "Britney Spears") ? "Britney Spears Company true" : "") %>
  <br>
  <%# ((Eval("NodeOwner").ToString() != "Xtina Aguilera") ? "Xtina Aguilera true" : "") %>
  <%# ((Eval("NodeOwner").ToString() != "Xtina Aguilera") ? "Xtina Aguilera Web Desinger " : "") %>
  <%# ((Eval("NodeOwner").ToString() != "Xtina Aguilera") ? "Xtina Aguilera Company true" : "") %>

The results I get is everything listed one very page:
Results

Britney Spears true Britney Spears Web Desinger true Britney Spears Company true Xtina Aguilera true Xtina Aguilera Web Desinger Xtina Aguilera Company true

How can I write it so that if NodeOwner matches it only shows the Results information of that owner.

Correct Answer

Trevor Fayas answered on March 13, 2017 18:21

Okay, this is what you will need.

<%# IfCompare(CMS.SiteProvider.UserInfoProvider.GetUserInfo((int)Eval("NodeOwner")).FullName, "Britney Spears", "not Britney Spears", "Is Britney Spears") %>

0 votesVote for this answer Unmark Correct answer

Recent Answers


Trevor Fayas answered on March 13, 2017 17:41 (last edited on March 13, 2017 17:50)

Have you tried just seeing what <% Eval("NodeOwner").ToString() %> resolves to? If it does'nt resolve to anything then all of your statements will be true.

The NodeOwner will also be an integer (the user id), so your logic won't ever resolve false because a number will never equal their names.

If you are using the Full name

<%# (GetUserFullName(Eval<int>("NodeOwner"), -1) != "Britney Spears") ? "Britney Spears true" : "") %>

Or using Text/html

{% (GlobalObjects.Users.Where("UserID = "+NodeOwner).FirstItem.FullName != "Britney Spears") ? "Britney Spears true" : "") %}

If you are looking up by Username, use the below

<%# (CMS.Membership.UserInfoProvider.GetUserFullName(CMS.Helpers.ValidationHelper.GetInteger(Eval("NodeOwner"), -1)) != "Britney Spears") ? "Britney Spears true" : "") %>

0 votesVote for this answer Mark as a Correct answer

Zachary Perry answered on March 13, 2017 17:42

Try to use IfCompare <%# IfCompare(Eval("NodeOwner"),"Britney Spears","No","Yes") %>

0 votesVote for this answer Mark as a Correct answer

Dcode warner answered on March 13, 2017 17:46

You right. It doesn't resolove to anything. However this gives me the user: <%#GetUserFullName(Eval<int>("NodeOwner"))%>

How can integrate that statement into my code above?

0 votesVote for this answer Mark as a Correct answer

Dcode warner answered on March 13, 2017 17:51

@ZacharyPerry Unfulaly. This shows eerywhere on both owners.

0 votesVote for this answer Mark as a Correct answer

Dcode warner answered on March 13, 2017 17:57

@Trevor Fayas I get errors for all of them.

If you are using the Full name [ERRROR] [CMSDataProperties.LoadTransformation]: http://server/CMSVirtualFiles/Transformations/=vg=ae353b51-fb2a-4e4e-972a-fd8ad18cecca/CMS.BlogPost/HospitalityUpgrade.ascx(27): error CS1002: ; expected

Or using Text/html

(Im not using text/html)

If you are looking up by Username, use the below [CMSDataProperties.LoadTransformation]: http://server/CMSVirtualFiles/Transformations/=vg=7846797b-e142-43bb-9c0b-5bd9e31f48af/CMS.BlogPost/HospitalityUpgrade.ascx(27): error CS1002: ; expected

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on March 13, 2017 18:07

Sorry, type on my part.

Try

<%# (GetUserFullName(Eval<int>("NodeOwner") != "Britney Spears") ? "Britney Spears true" : "") %>

Or

<%# IfCompare(GetUserFullName(Eval<int>("NodeOwner"), "Britney Spears"), "Not Britney Spears", "Is Britney Spears") %>

0 votesVote for this answer Mark as a Correct answer

Dcode warner answered on March 13, 2017 18:13

@Trevor Fayas Im still getting the following Errors. Is it because Im in Kentico 7?

  1. [CMSDataProperties.LoadTransformation]: http://server/CMSVirtualFiles/Transformations/=vg=071550b2-0dde-4ae5-970f-624f0c18257d/CMS.BlogPost/HospitalityUpgrade.ascx(28): error CS0019: Operator '!=' cannot be applied to operands of type 'int' and 'string'

  2. [CMSDataProperties.LoadTransformation]: http://server/CMSVirtualFiles/Transformations/=vg=ded31bb7-8f58-4f6b-8841-5484809ea031/CMS.BlogPost/HospitalityUpgrade.ascx(28): error CS1501: No overload for method 'GetUserFullName' takes 2 arguments

0 votesVote for this answer Mark as a Correct answer

Dcode warner answered on March 13, 2017 18:31

Thank you! This also works for me:

<%# IfCompare(Eval("NodeOwnerFullName"),"Britney Spears","No","Yes") %>

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on March 13, 2017 18:35

Must be an old Kentico 7 column, 8+ doesn't have it.

Please mark answer as answered, kind gesture for time spent figuring this out :)

0 votesVote for this answer Mark as a Correct answer

Dcode warner answered on March 13, 2017 18:50

Done thank you

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.