Link to home
Start Free TrialLog in
Avatar of richardsimnett
richardsimnett

asked on

how to use iff with a DataBinder.Eval

hi experts,
  hope you're all having as much fun as I am coding today :D
(man do i hate web coding)

anyway, i am trying to force a checkbox to be checked or not inside a Repeater control with the following statement:

<%# iif(DataBinder.Eval(Container.DataItem,"pickTeamID")=DataBinder.Eval(Container.DataItem,"teamID1"),"checked","") %>

this statemtent is sitting inside of an <input ...> tag and i am seeing an error that says:
Compiler Error Message: CS0131: The left-hand side of an assignment must be a variable, property or indexer

does it mean i need an == instead of =
or does it mean that its not possible to have it check against another column in the dataSet and that I should instead have the sproc return a computed value like 1 or 0 that i can check against like this:

<%# iif(DataBinder.Eval(Container.DataItem,"pickTeamID")=1,"checked","") %>

and if I am not even close feel free to point out I am an idiot and show me the way.

thanks.
Avatar of richardsimnett
richardsimnett

ASKER

forgot to mention this potentially useful tidbit:
  both pickTeamID and teamID1 are tinyint's in the db.  i dont know if that matters or what
richardsimnett,

Are you coding in C#?

-- Jason
richardsimnett,

IIf is a VB function, C# would be (I think this should work fine):

(DataBinder.Eval(Container.DataItem, "pickTeamID") == DataBinder.Eval(Container.DataItem, "teamID1")) ? "checked" : "";

-- Jason
yes i am using C#.  let me try what you suggested
richardsimnett,

Of course, while I'm at it, I'd recommend a <asp:CheckBox> control instead of <input>.  Maybe something like:

<asp:CheckBox runat="server" id="cbTeam" Checked='<%# (DataRowView)Container.DataItem["pickTeamID"] == (DataRowView)Container.DataItem["teamID1"] %>' />

I'm primarly a VB.NET programmer, so I'm kind of taking stabs at the C# syntax -- I believe it's mighty close, but might need some tweaking.

-- Jason
still getting an error about missing a )

here's the whole line of my code.  just in case i am missing something else

<td><input type=checkbox name=team_<%# DataBinder.Eval(Container.DataItem, "teamID1") %> id=team_<%# DataBinder.Eval(Container.DataItem, "teamID1") %> <%# (DataBinder.Eval(Container.DataItem, "pickTeamID") == DataBinder.Eval(Container.DataItem, "teamID1")) ? "checked" : ""; %>>&nbsp;&nbsp;
richardsimnett,

Whew, a syntax test, great!  I'll play, here's my try at it:

<input type='checkbox' name='<%# "team_" + DataBinder.Eval(Container.DataItem, "teamID1") %>' id='<%# "team_" + DataBinder.Eval(Container.DataItem, "teamID1") %>' <%# ((DataBinder.Eval(Container.DataItem, "pickTeamID") == DataBinder.Eval(Container.DataItem, "teamID1")) ? "checked" : "") %> />



-- Jason
thanks for being such a good sport.  and you are about 99% of the way there.

only one problem remains for me.  the == is returning false even when both sides of it have the same value.  is there a way to type cast it so it works?

for instance, can i use a Convert.ToInt32() wrapped around those DataBinder.Eval statements?

actually i just tried that and it gave me more compilation errors.

maybe a straight cast:

<%# ((int)(DataBinder.Eval(Container.DataItem, "pickTeamID") == (int)DataBinder.Eval(Container.DataItem, "teamID1")) ? "checked" : "") %>

nope.  adding the (int) doesnt work either.

any more ideas?
actually i had the (int) in the wrong place there, but it still didnt work when i fixed it to this:

<%# (((int)(DataBinder.Eval(Container.DataItem, "pickTeamID")) == (int)(DataBinder.Eval(Container.DataItem, "teamID2"))) ? "checked" : "") %> />
ASKER CERTIFIED SOLUTION
Avatar of Jason Scolaro
Jason Scolaro
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i added another databinder.eval to print the values out and they are indeed equal.

i tried it like this and the C# compiler didnt like it (i assume had i done int's it would have also crapped its pants)
public string CheckTeams(string pickTeam, string teamID)

so i changed it to objects and added the Convert and it ran perfectly.  i didnt even know you could call functions like that in your code behind class.  

you helped me big time today, jason.  i wish i could give you more than 500 points.  you earned it.

thanks.


public string CheckTeams(object pickTeam, object teamID)
            {
                  if (Convert.ToInt32(pickTeam) == Convert.ToInt32(teamID))
                  {
                        return "checked";
                  }
                  else
                  {
                        return "";
                  }
            }

Nice work, glad you got it going!  See you around.

-- Jason