0

This is probably really simple for someone who's familiar with Javascript, but I have having trouble calling a function while passing an eval as a parameter. I've got a bunch of dynamic checkboxes that I'm trying to create. I've got the text displaying great, but the checkbox is giving me trouble. The problem is, the value that I'm passing to the checkbox is a string (either "true" or "false") not a bool.

<asp:CheckBox runat="server" AutoPostBack="True" Checked='<%# Eval("Value") %>' Text='<%# Eval("DisplayName") %>'></asp:CheckBox>

I also created a short javascript function to convert it to boolean

function ConvertToBoolean(value)
{
     if (value == "true")
     {    return true;}
     if (value == "false")
     {    return false;}
 }

I'm not great with javascript, but I've tried everything I could find on SO. Not sure why it's not working for me. Here's what I've tried:

Checked='<%# Eval("Value") %>'
Checked="'<%# Eval("Value") %>'"
Checked='<%# "ConvertToBoolean(" + Eval("Value") + ");" %>'
Checked='<%# "ConvertToBoolean(\"" + Eval("Value") + "\");" %>'
Checked='<%# Eval("Value", "javascript:ConvertToBoolean({0});") %>'
Checked="ConvertToBoolean('<%# Eval("Value") %>')"

Am I misunderstanding something fundamental here? Is there even a way to do this?

9
  • what is "Value"? a C# variable? also ConvertToBoolean could be just return (value=="true"); Commented Jul 10, 2015 at 21:50
  • 3
    I fail to see the point on using Eval Commented Jul 10, 2015 at 21:50
  • @JohnKiller - yes, sorry I should have specified: it is a C# variable, it's from a list of objects (that include other properties like "DisplayName") that is bound to a DataList. (There are other items that are bound as well, aside from the checkbox, let me know if that is pertinent, I can add that code as well). The problem is, the object I'm binding has a string for the "value" instead of bool. Commented Jul 13, 2015 at 15:50
  • @JohnKiller - then is my syntax incorrect? With the code above (for the asp:checkbox) I'm getting "cannot convert string to a bool" error Commented Jul 13, 2015 at 16:46
  • @JohnKiller, I'm getting the following error: Cannot create an object of type 'System.Boolean' from its string representation '<%= (Value=="true") %>' for the 'Checked' property. I tried sticking "Eval" in there, but that didn't work either. Commented Jul 13, 2015 at 17:29

1 Answer 1

1
<asp:CheckBox runat="server" AutoPostBack="True" Checked='<%# Eval("Value")=="true" %>' Text='<%# Eval("DisplayName") %>' />

This code gets executed at server side and is translated to HTML, something like

<input type="checkbox" checked="checked"> .....

Ofcourse ASP needs to evaluate your Eval("Value")=="true" to decide if it needs to output checked or not

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.