Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I set disabled attribute on html textbox in asp.net-mvc?

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 } })%>

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?

like image 547
leora Avatar asked Aug 09 '10 19:08

leora


People also ask

How do I add a disabled attribute to a tag?

To set the disabled attribute, select the element and call the setAttribute() method on it, passing it disabled as the first parameter, e.g. button. setAttribute('disabled', '') . The setAttribute method will add the disabled attribute to the element.

What is disabled attribute in HTML?

The disabled attribute is a boolean attribute. When present, it specifies that the element should be disabled. A disabled element is unusable. The disabled attribute can be set to keep a user from using the element until some other condition has been met (like selecting a checkbox, etc.).


2 Answers

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.

like image 161
Robaticus Avatar answered Oct 22 '22 11:10

Robaticus


Actually it is possible to write an Extension class to the HtmlHelper to do this but you have to implement many overrides so the quickest solution I found was to write a dictionary extension.

You can use below class for this:

public static class DictionaryExtensions
{
    public static Dictionary<string, object> WithAttrIf(this Dictionary<string,object> dictionary,bool condition, string attrname, object value)
    {
        if (condition)
            dictionary[attrname] = value;

        return dictionary;
    }

    public static Dictionary<string, object> WithAttr(this Dictionary<string, object> dictionary, string attrname, object value)
    {

        dictionary[attrname] = value;

        return dictionary;
    }
}

To use it, import the class in your view and your view code looks like this:

@Html.TextBoxFor(m => m.FirstName,  new Dictionary<string, object>().WithAttr("class","input-large").WithAttrIf(!string.IsNullOrWhiteSpace(Model.FirstName),"readonly","yes"))

You can add as many attributes as you wish since the extension method adds the value to the dictionary and returns the dictionary itself

like image 30
Cagatay Kalan Avatar answered Oct 22 '22 10:10

Cagatay Kalan