2

SOLVED: See my solution below!

using aspx with C# code behind.

I have the following code in a button in a item template in a gridview:

Enabled='<%# IIF(Eval("wrqst_need_ind") == "Y","TRUE","FALSE") %>'

I am getting the following error:

The name 'IIF' does not exist in the current context

What am I doing wrong? I get the same error if I use "IF" rather than "IIF"

The full item template code is as follows:

<ItemTemplate>
                <asp:Button  ID="wrqst_need_ind_btn" runat="server" Text = "Create WR" 
                    onClientClick="javascript:popUp('popup_createWR.aspx')"  
                    Enabled='<%# IIF(Eval("wrqst_need_ind") == "Y","TRUE","FALSE") %>'
                    CommandArgument='<%# Eval("dvc_nm") + "|" + Eval("data_orgtn_yr") %>'/>
</ItemTemplate>

If I take the line out it works fine.

Seems to me like this should work...

EDIT: I am now using this:

Enabled='<%#Eval("wrqst_need_ind") == "Y" ? "TRUE" : "FALSE" %>'

And geting this error:

The server tag is not well formed.

Thanks so much for your help!

Update:

I tried this:

Enabled='<%# Eval("wrqst_need_ind") == "Y" ? Convert.ToBoolean(1) : Convert.ToBoolean(0) %>' and it ran!

But, every button was disabled. So I tried:

Enabled='<%# Eval("wrqst_need_ind") == "Y" ? Convert.ToBoolean(1) : Convert.ToBoolean(1) %>'

and then every button was disabled. It seems that every time it is returning false... why?

SOLVED: See my solution below!

kralco626
  • 8,456
  • 38
  • 112
  • 169

5 Answers5

7

In C#, that ternary syntax is:

Eval("wrqst_need_ind") == "Y" ? "TRUE" : "FALSE"

If you happen to be using VB.NET (it looks like you are not), use the If function.

If(Eval("wrqst_need_ind") == "Y", "TRUE", "FALSE")

EDIT: It turns out that == here will not compare the string contents, but the string objects. So, instead, you must use .Equals("Y"). So, community-generated final answer is:

Enabled='<%# Eval("wrqst_need_ind").Equals("Y") %>'
ChessWhiz
  • 4,656
  • 3
  • 26
  • 40
  • I'm getting an error converting string to bool... I'm trying a few different variations but i can't seem to get it... – kralco626 Jun 25 '10 at 16:19
  • my code is: Enabled='<%#Eval("wrqst_need_ind") == "Y" ? "TRUE" : "FALSE" %>' – kralco626 Jun 25 '10 at 16:19
  • I think you need a space before the "Eval" keyword. Also, I'm not sure if it's case-sensitive, but it should be "true" and "false". – ChessWhiz Jun 25 '10 at 16:26
  • stil getting the name error using the following code: Enabled='<%# Eval("wrqst_need_ind") == "Y" ? "true" : "false" %>' There is a space between the # and Eval and I made the true and flase lower case? Any other suggestions??? Thanks! – kralco626 Jun 28 '10 at 14:02
  • check off this as the answer because ChessWhiz should get the credit. However there is a minor technicality. You must use .Equals, not ==. I posted that change in my answer. – kralco626 Jun 28 '10 at 16:21
2

This is the code that worked!

Enabled='<%# Eval("wrqst_need_ind").ToString().Equals("Y".ToString()) ? Convert.ToBoolean(1) : Convert.ToBoolean(0) %>'
kralco626
  • 8,456
  • 38
  • 112
  • 169
  • 1
    Although as Chris noted this also works: Enabled='<%# Eval("wrqst_need_ind").Equals("Y") %>' Notice that you MUST have the .Equals or it will return false every time. (unless of course the objects are actually the same... but they are not) – kralco626 Jun 28 '10 at 14:32
  • I feel sad that you accepted your own answer, when it was really my answer that gave the solution to the original 'IIF' question. :) – ChessWhiz Jun 28 '10 at 15:46
  • wtf... i accepted your answer too... idk why it's not showing it... I was trying to give you credit and let other people know the answer that worked for me... – kralco626 Jun 28 '10 at 16:18
  • oh lol, guess you can't choose more than one answer as "the answer"... thats kinda silly, what if two people combined to give you the right answer? Well anyways, i checked your answer as the answer instead of mine... Thanks so much for your help :) – kralco626 Jun 28 '10 at 16:19
  • well i commented on your answer about the .Equals thing so others would see it. That so stupid, I should be able to mark two answers as the answer and have them both apear at the top so people can see the right answerS first... – kralco626 Jun 28 '10 at 16:23
1

I'm not sure about all the "Convert.ToBoolean(1)" stuff you've got going on. If you want true and false just write true and false (without quotes or it will be treated as a string).

eg. Enabled='<%#Eval("wrqst_need_ind") == "Y" ? true:false %>'

Of course as I type that it occurs to me that you don't need to use this kind of operator. the condition returns true or false anyway so the above simplifies to:

Enabled='<%#Eval("wrqst_need_ind") == "Y" %>'
Chris
  • 27,210
  • 6
  • 71
  • 92
  • ya not using true or false at all might have worked. However I tried not using quotes and I got some other error. I don't remeber what the error was I tried the other day. However, because the comparison is between two strings .Equals must be used or the expression will always return false. – kralco626 Jun 28 '10 at 14:26
0

In case it will help anyone else...

This style did not work for me:

(Eval("status").ToString()).Equals("Done")

but this style did work:

(Eval("status").ToString()).Contains("Done")
Warren Stevens
  • 1,047
  • 12
  • 12
0

I'm pretty sure the correct keywork is "if" in lowercase.

ALOToverflow
  • 2,679
  • 5
  • 36
  • 70