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’
If you supply
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.
Code behind: