views:

208

answers:

2

i am trying to dynamically set the disabled attribute on the html textbox and having issues

i tried this in my view:

 string disabledString = "";
 if (SomeLogic)
 {
      disabledString = "disabled";
 }

 Html.Textbox()...new Dictionary<string, object> { { "maxlength", 50 }, { "disabled", readOnlyState } })%>

so as you can see i am setting the disabled attribute to "" or disabled but when i test, it seems to be disabled in either case. am i missing something?

A: 

I think you want to omit the disabled attribute altogether when you want it to be enabled. Older browsers would look at the following and disable the text boxes:

<input type="text" disabled></input>

In other words in older HTML the ="disabled" was not necessary so for compatibility reasons you should just omit the attribute if you want it to render right. I'm not sure what happens if you try a strict DOCTYPE, though.

Dismissile
@Dismissile - i need to use the above format. that is where i am getting stuck on how to add conditional attribute using this html.textbox format ?
ooo
+2  A: 

This was ugly for us, due to the fact that the HTML spec is lousy here.

Basically in our view code we had some logic like this:

bool isPlatformOwner = false;

object disabledAttributes = new { @disabled="disabled", @readonly="readonly" };

//omitted code setting isPlatformOwner    

    if (isPlatformOwner)
    {
        disabledAttributes = new { };
    }

Then, for our controls, we had this:

<%=Html.CheckBoxFor(f => f.AddToReleaseIndicator, disabledAttributes)%>

Anonymous types saved us here, but, like I said, it got a little ugly.

Robaticus