tags:

views:

147

answers:

2

Here is a simplified example of what I am trying to achieve;

<asp:CheckBox runat="server" Checked="<%# this.isChecked %>" id="myCheckBox" />

Then in my code behind;

public partial class _Default : System.Web.UI.Page 
{
    bool _isChecked = true;

    public string isChecked
    {
     get
     {
      return _isChecked.ToString();
     }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    }
}

I am aware that, in this case, I can simply use myCheckBox.Checked = true, but I'm just using this example for illustration purposes. The real control is a custom one, but I'm not having any luck getting this approach to work. It just seems to ignore the value I'm supplying.

Is it valid to use <%# this.isChecked %> in this manner?

EDIT:

Sorry, I wrote this in a hurry and realise that it's not really clear that I intended "checkbox" to just be an example. I'm just trying to get a server-side bool into any particular attribute of the ASP control, not necessarily the "checked" attribute of the checkbox control.

EDIT 2: Updated my example to use string property rather than boolean field, but it is reporting the following error in my ASPX file now;

Cannot convert type 'string' to 'bool'

+3  A: 

If you supply

checked="anything"

it will be checked (in HTML). You need to omit the checked attribute for it to not be checked, so this will not work.


After the edit to the question, here is my updated answer:

I would not try to set the attribute in the aspx page, rather I would use the ID and do it in code.

eg.

<asp:Control ID="uxMyControl" runat="server" attributes="other"... />

Code behind:

private Boolean mypropery = false;

Page_Load(object sender, EventArgs e)
{
    this.myproperty = true;
    uxMyControl.attributes = myproperty;
}
ck
This is what I had initially done, but because the control is rendering before Page_Load for the parent page gets executed, the attribute is set too late, and the control is rendered according to the default value of the attribute, rather than the one set in Page_Load.
C.McAtackney
The control shouldn't be fully rendered at that point, however have you tried editing the Page_load of the control? Or using Page_PreInit?
ck
'Checked' is a boolean property on a server control in this case, not an html attribute. The rendering is handled by the server control.
Adam Lassek
@Adam Lassek which is why the property is set in the code behind - the name "attributes" was probably a poor choice, however the fact I didn't use it with an indexer or capitalise correctly suggests it isn't being used to modify HTML attributes.
ck
+2  A: 

No, because you're returning a bool rather than the string (typecasting problem), you need a property rather than a field to work with the databinding expression, and some browsers won't correctly render your checkbox with the words "true/false". Instead, they expect to see checked="checked" to check the box or nothing to un-check it.

Based on your edit, I suspect you have code elsewhere that is trying to set the isChecked boolean. The code fails now that the isChecked name points to a string property instead. Update it to set _isChecked.

Joel Coehoorn
I've changed from a bool field to a string property, but now I'm getting "Cannot convert type 'string' to 'bool'" in my .aspx file. Any idea why it's not accepting the string?
C.McAtackney
Several ideas, but the simplest way to know which one is right is if you edit your question to show the new code.
Joel Coehoorn
Original question updated with revised code
C.McAtackney
Odd: the code you posted looks okay. The error makes it sound like you're trying to assign a string to the bool backing store at some point, but you don't have a setter in your code anywhere.
Joel Coehoorn
The code I've posted in the first post is actually an entire project (minus the extra HTML stuff in the ASPX file). I think what's happening is that .NET really is expecting a bool, not a string, to be supplied there. Now, why it won't USE one when I supply it a bool.. that's the head scratcher.
C.McAtackney