views:

40

answers:

4

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!

A: 

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

Frank
He's using VB, not C# :) `IIF` = "If and only if"
Nick Craver
i'm not using vb. i'm using C#. Or at least that was my intention.
kralco626
+3  A: 

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
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
my code is: Enabled='<%#Eval("wrqst_need_ind") == "Y" ? "TRUE" : "FALSE" %>'
kralco626
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
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
I made some progress... see update above...
kralco626
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
A: 

This is the code that worked!

Enabled='<%# Eval("wrqst_need_ind").ToString().Equals("Y".ToString()) ? Convert.ToBoolean(1) : Convert.ToBoolean(0) %>'
kralco626
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
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
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
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
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
+1  A: 

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
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